/
manage.py
122 lines (94 loc) · 3.96 KB
/
manage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# Copyright (c) 2020 Kiel University and others.
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# http://www.eclipse.org/legal/epl-2.0.
#
# SPDX-License-Identifier: EPL-2.0
"""Helper script to manage our downloads area.
This script currently only lists files and directories in our downloads area on the
Eclipse download servers. Future versions could support removing files and directories
as well. If someone adds such functionality, it should default to printing what it
would do first, and require an explicit command line switch to make it actually do it.
"""
import argparse
from pathlib import Path
import shutil
# The default directory all path operations will be relative to.
DEFAULT_BASE_DIR = "/home/data/httpd/download.eclipse.org/elk/"
#
# # #### #####
# # # #
# # #### #
# # # #
# # # # #
####### # #### #
def print_path(path: Path, curr_depth: int, max_depth: int) -> None:
"""Prints the name of the given path to the console, with the correct indentation. If
the path happens to be a directory and we have not reached the maximum depth yet, we
call ourself recursively on its content as well."""
if path.is_file():
print((" " * curr_depth) + F"f {path.name}")
elif path.is_dir():
# The name of the depth-0 directory should be printed in full
if curr_depth == 0:
path_name = str(path)
else:
path_name = path.name
print((" " * curr_depth) + F"d {path_name}")
# Check if we need to recurse
if curr_depth < max_depth:
for d in [x for x in path.iterdir() if x.is_dir()]:
print_path(d, curr_depth + 1, max_depth)
for f in [x for x in path.iterdir() if x.is_file()]:
print_path(f, curr_depth + 1, max_depth)
def cmd_list(args: argparse.Namespace) -> None:
print_path(Path(args.base_path), 0, args.depth)
# #
## ## #### # # ######
# # # # # # # # #
# # # # # # # #####
# # # # # # #
# # # # # # #
# # #### ## ######
def cmd_mv(args: argparse.Namespace) -> None:
shutil.move(args.source, args.dest)
##### # ######
# # # # ##### # # # # ###### # # ## ##### #### # # # ####
# ## ## # # # # ## # # # # # # # # # # ## # # #
# # ## # # # # # # # # ##### ###### # # # # #### # # # # #
# # # # # # # # # # # # ###### ##### # # # # # # ###
# # # # # # # # # ## # # # # # # # # # # ## # #
##### # # ##### ####### # # # ###### # # # # # #### # # # ####
# Setup the main parser
parser = argparse.ArgumentParser(description="Manages folders on the download server.")
parser.add_argument(
"--base-path", "-bp",
default=DEFAULT_BASE_DIR,
help="Sets the base directory all other commands operate on.")
subparsers = parser.add_subparsers(help="Available commands.")
# The list command
parser_list = subparsers.add_parser(
"list",
help="Lists files and folders in and, optionally, below the base folder.")
parser_list.set_defaults(func=cmd_list)
parser_list.add_argument(
"--depth",
type=int,
default=1,
help="How many folder levels to go down. By default, only the base folder's content is listed.")
# The move command
parser_move = subparsers.add_parser(
"mv",
help="Move a folder to another location.")
parser_move.set_defaults(func=cmd_mv)
parser_move.add_argument(
"source",
type=str,
help="Path to the folder to move.")
parser_move.add_argument(
"dest",
type=str,
help="Path to the folder's new parent folder.")
# Go parse stuff!
args = parser.parse_args()
args.func(args)