Coverage for pyquickhelper/cli/encryption_file_cli.py: 100%

35 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-03 02:21 +0200

1""" 

2@file 

3@brief encrypt and decrypt command lines for just a file 

4""" 

5from __future__ import print_function 

6import os 

7import argparse 

8import sys 

9 

10 

11def get_parser(encrypt): 

12 """ 

13 defines the way to parse the magic command ``%encrypt`` and ``%decrypt`` 

14 

15 @param encrypt encrypt or decrypt 

16 @return parser 

17 """ 

18 task = "encrypt_file" if encrypt else "decrypt_file" 

19 parser = argparse.ArgumentParser(prog=task, 

20 description=f'{task} a file' + 

21 '\ndoes not work well in Python 2.7 with pycryptodome') 

22 parser.add_argument( 

23 'source', 

24 help=f'file to {task}') 

25 parser.add_argument( 

26 'dest', 

27 help=f'location of the {task}ed file') 

28 parser.add_argument( 

29 'password', 

30 help='password, usually an ascii string with 16x characters') 

31 

32 return parser 

33 

34 

35def do_main(source, dest, password, encrypt, fLOG=None): 

36 """ 

37 Encrypt or decrypt of a file 

38 

39 @param source source of files to encrypt or decrypt 

40 @param dest destination 

41 @param password password 

42 @param encrypt boolean, True to encrypt 

43 @param fLOG logging function 

44 """ 

45 if not os.path.exists(source): 

46 raise FileNotFoundError(source) # pragma: no cover 

47 try: 

48 from pyquickhelper.filehelper import encrypt_stream, decrypt_stream 

49 except ImportError: # pragma: no cover 

50 folder = os.path.normpath(os.path.join( 

51 os.path.abspath(os.path.dirname(__file__)), "..", "..")) 

52 sys.path.append(folder) 

53 from pyquickhelper.filehelper import encrypt_stream, decrypt_stream 

54 

55 if isinstance(password, str): 

56 password = bytes(password, encoding="ascii") 

57 

58 if encrypt: 

59 encrypt_stream(key=password, 

60 filename=source, 

61 out_filename=dest, 

62 chunksize=os.stat(source).st_size * 2 + 1) 

63 else: 

64 decrypt_stream(key=password, 

65 filename=source, 

66 out_filename=dest, 

67 chunksize=os.stat(source).st_size * 2 + 1) 

68 

69 

70def encrypt_file(fLOG=print, args=None): 

71 """ 

72 Encrypts using class @see fn encrypt_stream. 

73 

74 @param fLOG logging function 

75 @param args to overwrite ``sys.args`` 

76 

77 .. cmdref:: 

78 :title: Encrypt a file 

79 :cmd: pyquickhelper.cli.encryption_file_cli:encrypt_file 

80 

81 Encrypt a file from the command line. 

82 """ 

83 parser = get_parser(True) 

84 if args is not None and args == ['--help']: 

85 fLOG(parser.format_help()) # pragma: no cover 

86 else: 

87 try: 

88 args = parser.parse_args() 

89 except SystemExit: # pragma: no cover 

90 if fLOG: 

91 fLOG(parser.format_usage()) 

92 args = None 

93 

94 if args is not None: 

95 do_main(source=args.source, dest=args.dest, 

96 password=args.password, encrypt=True, 

97 fLOG=fLOG) 

98 

99 

100def decrypt_file(fLOG=print, args=None): 

101 """ 

102 Decrypts using class @see fn decrypt_stream. 

103 

104 @param fLOG logging function 

105 @param args to overwrite ``sys.args`` 

106 

107 .. cmdref:: 

108 :title: Decrypt a file 

109 :cmd: pyquickhelper.cli.encryption_file_cli:decrypt_file 

110 

111 Decrypt a file from the command line. 

112 """ 

113 parser = get_parser(False) 

114 if args is not None and args == ['--help']: 

115 fLOG(parser.format_help()) # pragma: no cover 

116 else: 

117 try: 

118 args = parser.parse_args() 

119 except SystemExit: # pragma: no cover 

120 if fLOG: 

121 fLOG(parser.format_usage()) 

122 args = None 

123 

124 if args is not None: 

125 do_main(source=args.source, dest=args.dest, 

126 password=args.password, encrypt=False, 

127 fLOG=fLOG) 

128 

129 

130if __name__ == "__main__": 

131 decrypt_file() # pragma: no cover