Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1"""LICENSE 

2Copyright 2015 Hermann Krumrey <hermann@krumreyh.com> 

3 

4This file is part of toktokkie. 

5 

6toktokkie is free software: you can redistribute it and/or modify 

7it under the terms of the GNU General Public License as published by 

8the Free Software Foundation, either version 3 of the License, or 

9(at your option) any later version. 

10 

11toktokkie is distributed in the hope that it will be useful, 

12but WITHOUT ANY WARRANTY; without even the implied warranty of 

13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

14GNU General Public License for more details. 

15 

16You should have received a copy of the GNU General Public License 

17along with toktokkie. If not, see <http://www.gnu.org/licenses/>. 

18LICENSE""" 

19 

20import os 

21import argparse 

22import shutil 

23from toktokkie.commands.Command import Command 

24from toktokkie.Directory import Directory 

25from puffotter.os import makedirs 

26 

27 

28class ArchiveCommand(Command): 

29 """ 

30 Class that encapsulates behaviour of the archive command 

31 """ 

32 

33 @classmethod 

34 def name(cls) -> str: 

35 """ 

36 :return: The command name 

37 """ 

38 return "archive" 

39 

40 @classmethod 

41 def help(cls) -> str: 

42 """ 

43 :return: The help message for the command 

44 """ 

45 return "Archives the folder structure and metadata" 

46 

47 @classmethod 

48 def prepare_parser(cls, parser: argparse.ArgumentParser): 

49 """ 

50 Prepares an argumentparser for this command 

51 :param parser: The parser to prepare 

52 :return: None 

53 """ 

54 cls.add_directories_arg(parser) 

55 parser.add_argument("--out", "-o", default=None, 

56 help="Specifies an output directory for the " 

57 "archived directory/directories") 

58 parser.add_argument("--remove-icons", action="store_true", 

59 help="Replaces icon files with empty files") 

60 

61 def execute(self): 

62 """ 

63 Executes the commands 

64 :return: None 

65 """ 

66 output_path = self.args.out 

67 

68 if output_path is not None: 

69 makedirs(output_path) 

70 

71 for directory in Directory.load_directories(self.args.directories): 

72 if output_path is None: 

73 archive_path = directory.path + ".archive" 

74 else: 

75 archive_path = os.path.join( 

76 output_path, directory.metadata.name 

77 ) 

78 if not os.path.isdir(archive_path): 

79 os.makedirs(archive_path) 

80 self.archive(directory.path, archive_path) 

81 

82 def archive(self, source: str, dest: str): 

83 """ 

84 Creates a low-filesize archive of a directory into a new directory 

85 :param source: The source directory 

86 :param dest: The destination directory 

87 :return: None 

88 """ 

89 self.logger.info("{} -> {}".format(source, dest)) 

90 

91 if os.path.basename(source) == ".wine": # Ignore .wine directories 

92 return 

93 

94 try: 

95 for child in os.listdir(source): 

96 child_path = os.path.join(source, child) 

97 dest_child_path = os.path.join(dest, child) 

98 

99 if os.path.isfile(child_path): 

100 

101 if child_path.endswith(".json"): 

102 shutil.copyfile(child_path, dest_child_path) 

103 elif child_path.endswith(".png"): 

104 if self.args.remove_icons: 

105 with open(dest_child_path, "w") as f: 

106 f.write("") 

107 else: 

108 shutil.copyfile(child_path, dest_child_path) 

109 else: 

110 with open(dest_child_path, "w") as f: 

111 f.write("") 

112 

113 elif os.path.isdir(child_path): 

114 if not os.path.isdir(dest_child_path): 

115 os.makedirs(dest_child_path) 

116 

117 self.archive(child_path, dest_child_path) 

118 except PermissionError: 

119 pass