-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Egor Kovetskiy <[email protected]>
- Loading branch information
Showing
18 changed files
with
227 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#!/bin/env python3 | ||
|
||
import os | ||
import re | ||
import sys | ||
|
||
def update_version(file_path, version_type): | ||
with open(file_path, 'r') as file: | ||
content = file.read() | ||
|
||
version_pattern = r'(version\s*=\s*")(\d+\.\d+\.\d+)(")' | ||
match = re.search(version_pattern, content) | ||
|
||
if match: | ||
current_version = match.group(2) | ||
major, minor, patch = map(int, current_version.split('.')) | ||
|
||
if version_type == 'major': | ||
major += 1 | ||
minor = 0 | ||
patch = 0 | ||
elif version_type == 'minor': | ||
minor += 1 | ||
patch = 0 | ||
elif version_type == 'patch': | ||
patch += 1 | ||
else: | ||
print(f"Invalid version type: {version_type}") | ||
return | ||
|
||
new_version = f"{major}.{minor}.{patch}" | ||
new_content = re.sub(version_pattern, r'\g<1>' + new_version + r'\g<3>', content) | ||
|
||
with open(file_path, 'w') as file: | ||
file.write(new_content) | ||
|
||
print(f"Updated {file_path}: {current_version} -> {new_version}") | ||
else: | ||
print(f"No version found in {file_path}") | ||
|
||
def update_versions_in_directory(directory, version_type): | ||
for root, dirs, files in os.walk(directory): | ||
for file in files: | ||
if file.endswith('.go'): | ||
file_path = os.path.join(root, file) | ||
update_version(file_path, version_type) | ||
|
||
if __name__ == '__main__': | ||
if len(sys.argv) != 3: | ||
print("Usage: python script.py <directory> <version_type>") | ||
print("version_type can be 'major', 'minor', or 'patch'") | ||
sys.exit(1) | ||
|
||
directory = sys.argv[1] | ||
version_type = sys.argv[2] | ||
|
||
update_versions_in_directory(directory, version_type) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#!/usr/bin/env python3 | ||
"""frename - File and Directory Renaming Tool | ||
This script recursively renames files and directories using sed. | ||
Usage: | ||
frename [options] <regexp> [<dir>] | ||
Options: | ||
-h --help Show this help message and exit. | ||
-v --verbose Increase verbosity of output. | ||
Arguments: | ||
<regexp> Regular expression for renaming (sed-style substitution). | ||
<dir> Directory to start from [default: current directory]. | ||
""" | ||
|
||
from docopt import docopt | ||
import os | ||
import sys | ||
import subprocess | ||
|
||
def rename_with_sed(old_path, regexp, verbose=False): | ||
try: | ||
new_name = subprocess.check_output(['sed', '-r', regexp], input=os.path.basename(old_path), universal_newlines=True).strip() | ||
new_path = os.path.join(os.path.dirname(old_path), new_name) | ||
|
||
if old_path != new_path: | ||
os.rename(old_path, new_path) | ||
if verbose: | ||
print(f"Renamed: {old_path} -> {new_path}") | ||
|
||
return new_path | ||
except subprocess.CalledProcessError as e: | ||
print(f"Error applying sed to {old_path}: {e}", file=sys.stderr) | ||
sys.exit(1) | ||
except OSError as e: | ||
print(f"Error renaming {old_path}: {e}", file=sys.stderr) | ||
|
||
return old_path | ||
|
||
def rename_recursive(start_dir, regexp, verbose=False): | ||
for root, dirs, files in os.walk(start_dir, topdown=False): | ||
# Rename directories first (bottom-up) | ||
for i, name in enumerate(dirs): | ||
old_path = os.path.join(root, name) | ||
new_path = rename_with_sed(old_path, regexp, verbose) | ||
dirs[i] = os.path.basename(new_path) | ||
|
||
# Then rename files | ||
for name in files: | ||
old_path = os.path.join(root, name) | ||
rename_with_sed(old_path, regexp, verbose) | ||
|
||
# Rename the start directory if needed | ||
if start_dir != '.': | ||
rename_with_sed(start_dir, regexp, verbose) | ||
|
||
if __name__ == '__main__': | ||
arguments = docopt(__doc__) | ||
regexp = arguments['<regexp>'] | ||
start_dir = arguments['<dir>'] or '.' | ||
verbose = arguments['--verbose'] | ||
|
||
if not os.path.exists(start_dir): | ||
print(f"Error: Directory '{start_dir}' does not exist.", file=sys.stderr) | ||
sys.exit(1) | ||
|
||
rename_recursive(start_dir, regexp, verbose) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.