Skip to content

Commit

Permalink
fix: glob pattern start from rootdir (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
Snoopy1866 authored Jan 3, 2025
1 parent c35f5a5 commit 249a686
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,12 @@ submit copydir "/source" "/dest" --exclude-dirs macro --exclude-files fcmp.sas f

你可以在路径中使用以下特殊字符作为通配符:

- `*`: 匹配零个或多个字符,但不匹配路径分隔符 `/`。例如,`f*.sas` 匹配 `f1.sas``f2.sas``f3.sas` 等等。
- `**`: 匹配零个或多个任意字符。例如,`**/f*.sas` 匹配 `figure/f1.sas``figure/f2.sas``figure/draft/f1.sas` 等等。
- `?`: 匹配零个或一个任意字符。例如,`t1?.sas` 匹配 `t1.sas``t10.sas``t11.sas` 等等。
- `[]`: 匹配括号中列出的或包括在范围内的任一字母数字字符。范围只能包含 `a-z``A-Z``0-9`。例如,`t[1-3]0.sas` 匹配 `t10.sas``t20.sas``t30.sas`
- `*`: 匹配任意数量的非分隔符型字符,包括零个。例如,`f*.sas` 匹配 `f1.sas``f2.sas``f3.sas` 等等。
- `**`: 匹配任意数量的文件或目录分段,包括零个。例如,`**/f*.sas` 匹配 `figure/f1.sas``figure/f2.sas``figure/draft/f1.sas` 等等。
- `?`: 匹配一个不是分隔符的字符。例如,`t1?.sas` 匹配 `t1.sas``t10.sas``t11.sas` 等等。
- `[seq]`: 匹配在 seq 中的一个字符。例如,`[tfl]1.sas` 匹配 `t1.sas``f1.sas``l1.sas`

更多语法请查看 [模式语言](https://docs.python.org/zh-cn/3/library/pathlib.html#pattern-language)

假设有这样一个文件目录结构:

Expand Down Expand Up @@ -317,7 +319,7 @@ submit copydir source dest --exclude-files "f2-deprecated.sas" "f3-deprecated.sa
使用 `glob` 模式,命令得到简化:

```bash
submit copydir source dest --exclude-files "*-deprecated*.sas"
submit copydir source dest --exclude-files "*deprecated*.sas"
```

### 命令行选项参考
Expand Down
13 changes: 9 additions & 4 deletions submit.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,18 +156,23 @@ def copy_directory(
if exclude_files:
exclude_files = expand_glob_patterns(exclude_files, root_dir=sas_dir)

# 转换 SAS 文件
for dirpath, _, filenames in os.walk(sas_dir):
if exclude_dirs is not None and os.path.split(dirpath)[-1] in exclude_dirs:
dirrelpath = os.path.relpath(dirpath, sas_dir)
if exclude_dirs is not None and dirrelpath in exclude_dirs:
continue
if os.path.commonpath([dirpath, sas_dir]) == txt_dir: # 如果当前目录是目标目录或其子目录,则跳过
continue
ref_path = os.path.relpath(dirpath, sas_dir)
for file in filenames:
if exclude_files is not None and file in exclude_files:
filerelpath = os.path.join(dirrelpath, file)
if exclude_files is not None and filerelpath in exclude_files:
continue
if file.endswith(".sas"):
sas_file = os.path.join(dirpath, file)
txt_file = os.path.join(txt_dir, ref_path, file.replace(".sas", ".txt"))
txt_file = os.path.join(txt_dir, os.path.join(txt_dir, dirrelpath), file.replace(".sas", ".txt"))
copy_file(sas_file, txt_file, convert_mode=convert_mode, macro_subs=macro_subs, encoding=encoding)

# 合并文件
if merge is not None:
merge_file = os.path.join(txt_dir, merge)
with open(merge_file, "w", encoding=encoding) as f:
Expand Down

0 comments on commit 249a686

Please sign in to comment.