Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial Rust junk #358

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@
/.coverage
/.tox
/dist


# Added by cargo

/target
108 changes: 108 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "identify"
version = "0.0.1"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
phf = { "version" = "0.11.1", "features" = ["macros"] }
78 changes: 78 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use std::collections::HashMap;
use std::collections::HashSet;
use std::env;
use std::fs;
use std::path::Path;

type Dict = HashMap<String, HashSet<String>>;

fn serialize_map(map: Dict, filename: &Path) {
let mut lines: Vec<String> = ["phf_map!(\n".into()].into();
for (ext, tags) in map.iter() {
lines.push(format!(r#" "{ext}" => phf_set!("#));
for tag in tags {
lines.push(format!(r#""{tag}", "#));
}
lines.push("),\n".into());
}
lines.push(")".into());
fs::write(filename, lines.join("")).unwrap();
}

fn main() {
// We want to create a series of hashmaps from
// identify/{extensions,interpreters}.py
// and place them in `out_dir/{extensions,interpreters}.rs`
// (or name each file after the dict, I suppose)

let mut extensions: Dict = HashMap::new();
let mut extensions_need_binary_check: Dict = HashMap::new();
let mut names: Dict = HashMap::new();
let mut interpreters: Dict = HashMap::new();
let mut current_dict = String::new();

// take a python file
let mut python = fs::read_to_string("identify/extensions.py").unwrap();
python.push_str(&fs::read_to_string("identify/interpreters.py").unwrap());

// read the dicts into hashmaps
for line in python.lines() {
if let Some((dict_name, _)) = line.split_once('=') {
current_dict = dict_name.trim().into();
}
else if let Some((ext, tags)) = line.split_once(':') {
let ext = ext.trim().replace('\'', "").to_string();
let tags: HashSet<String> = tags.trim()
.split(',')
.map(|tag|
tag.trim().replace(|c| "'{}".contains(c), "")
)
.filter(|tag| !tag.is_empty())
.collect();

match current_dict.as_str() {
"EXTENSIONS" => extensions.insert(ext, tags),
"EXTENSIONS_NEED_BINARY_CHECK" => {
extensions_need_binary_check.insert(ext, tags)
},
"NAMES" => names.insert(ext, tags),
"INTERPRETERS" => interpreters.insert(ext, tags),
_ => panic!("Unexpected dict name: {current_dict}"),
};
}
}

// write them into a rust file
let out_dir = env::var_os("OUT_DIR").unwrap();

let extensions_rs = Path::new(&out_dir).join("extensions.rs");
let enbc_rs = Path::new(&out_dir).join("extensions_need_binary_check.rs");
let names_rs = Path::new(&out_dir).join("names.rs");
let interpreters_rs = Path::new(&out_dir).join("interpreters.rs");
serialize_map(extensions, &extensions_rs);
serialize_map(extensions_need_binary_check, &enbc_rs);
serialize_map(names, &names_rs);
serialize_map(interpreters, &interpreters_rs);

println!("cargo:rerun-if-changed=build.rs");
}
155 changes: 155 additions & 0 deletions src/identify.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
#![allow(dead_code)]
#![allow(unused_imports)]
#![allow(unused_variables)]
#![allow(unused_mut)]

use std::collections::HashMap;
use std::collections::HashSet;
use std::ffi::OsStr;
use std::ffi::OsString;
use std::fs;
use std::os::unix::fs::FileTypeExt; // For `filetype.is_socket()` apparently
// use std::os::unix::fs::PermissionsExt; // fs::Permissions `mode()`
use std::os::unix::fs::MetadataExt; // fs::Permissions `mode()`
use std::path::Path;

use crate::tags;

#[derive(Debug, Eq, Hash, PartialEq)]
pub enum Tags {
Directory,
Symlink,
Socket,
File,
Executable,
NonExecutable,
Text,
Binary,
}

pub fn tags_from_path(file_path: &str) -> HashSet<Tags> {
let file = Path::new(file_path);
// TODO: Convert to Error
if !file.exists() {
panic!("{file_path} does not exist.");
}

let metadata = fs::symlink_metadata(&file);

if let Ok(metadata) = metadata {
// let perms = metadata.mode() & 0o777;
// println!("{:o}", perms);
if metadata.is_symlink() {
return HashSet::from([Tags::Symlink]);
}
if metadata.is_dir() {
return HashSet::from([Tags::Directory]);
}
if metadata.file_type().is_socket() {
return HashSet::from([Tags::Socket]);
}
}

let tags = HashSet::from([Tags::File]);
// TODO
// If executable, add to `tags`

let t = tags_from_filename(file_path);
// see if we can get tags_from_filename() and if not,
// then... weird parse_shebang stuff?

// a lil more. reread it when not tired.
tags
}

pub fn tags_from_filename(filename: &str) -> HashSet<String> {
let path = Path::new(filename);
let filename = path.file_name().unwrap().to_str().unwrap().to_string();
let ext = path.extension().unwrap().to_str().unwrap().to_lowercase();

let mut ret = HashSet::new();
/*
let _: Vec<&str> = filename.split('.').collect();
let mut parts = Vec::from([filename.clone()]);
parts.extend(filename.split('.').map(|s| s.to_string()));

for part in parts {
if tags::NAMES.contains_key(&part) {
println!("{:?}", tags::NAMES[&part]);
// ret.push(tags::NAMES[&part]);
}
println!("Boop: {}", part);
}
*/

if tags::EXTENSIONS.contains_key(&ext) {
ret.extend(tags::EXTENSIONS[&ext].iter().map(|s| s.to_string()));
} else if tags::EXTENSIONS_NEED_BINARY_CHECK.contains_key(&ext) {
ret.extend(
tags::EXTENSIONS_NEED_BINARY_CHECK[&ext]
.iter()
.map(|s| s.to_string()),
);
}
/*
for part in Vec::from([
filename.clone(),
filename.split('.').map(|s| s.to_string()).collect()
]) {
println!("Boop: {}", part);
}
*/

// identify.py creates a set, then,
// if filename + filename.split('.') items in extensions.NAMES,
// add to set and break
/*
let mut map = HashSet::new();
if filename in extensions::names() {
map.insert(extension);
}
*/

// if there's an extension,
// lowercase it,
// then if it's in extension.EXTENSIONS, add to set
// or if it's in extension.EXTENSIONS_NEED_BINARY_CHECK, add to set
// return set

/*
let mut tags: HashSet<String> = HashSet::new();
if let Some(name) = path.file_name().and_then(OsStr::to_str) {
tags.insert(name.to_owned());
}
if let Some(ext) = path.extension().and_then(OsStr::to_str) {
tags.insert(ext.to_owned());
}
*/
// Get filename and extension
// Allow "Dockerfile.xenial" to also match "Dockerfile"
// If filename in extensions.NAMES, add
// If extension in EXTENSIONS, add
// tags
ret
}

pub fn tags_from_interpreter(interpreter: &str) -> HashSet<String> {
HashSet::new()
}

pub fn is_text(/* bytes io */) -> bool {
false
}

pub fn file_is_text(path: &str) -> bool {
false
}

/*
pub fn parse_shebang( /* bytesio */) -> tuple of unknown size? {
}


pub fn parse_shebang_from_file(path: PathBuf) -> tuple of unknown size? {
}
*/
18 changes: 18 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use std::env;

mod identify;
mod tags;

fn main() {
let args: Vec<String> = env::args().skip(1).collect();
if args.len() < 1 {
eprintln!("Usage: identify [--filename-only] FILE");
return;
}

if args[0] == "--filename-only" {
println!("{:?}", identify::tags_from_filename(&args[1]));
} else {
println!("{:?}", identify::tags_from_path(&args[0]));
}
}
15 changes: 15 additions & 0 deletions src/tags.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use phf::phf_map;
use phf::phf_set;


pub const NAMES: phf::Map<&str, phf::Set<&str>> =
include!(concat!(env!("OUT_DIR"), "/names.rs"));

pub const EXTENSIONS: phf::Map<&str, phf::Set<&str>> =
include!(concat!(env!("OUT_DIR"), "/extensions.rs"));

pub const EXTENSIONS_NEED_BINARY_CHECK: phf::Map<&str, phf::Set<&str>> =
include!(concat!(env!("OUT_DIR"), "/extensions_need_binary_check.rs"));

pub const INTERPRETERS: phf::Map<&str, phf::Set<&str>> =
include!(concat!(env!("OUT_DIR"), "/interpreters.rs"));