-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathpacman_diff.py
executable file
·118 lines (100 loc) · 4.59 KB
/
pacman_diff.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
#!/usr/bin/env python2
# -*- coding: utf8 -*-
"""
pacman_diff.py: Report changes in the packages installed on a Arch based sys
"""
#==============================================================================
# This script is intended to run periodically through cron. This generates a
# list of packages installed on your system, and compares it with the one
# generated in the previous run. If there are differences, then generates a
# report that is saved to disk and sent by mail to the user who scheduled the
# cron job. It checks the Linux Arch packaging system, and therefore works on
# Arch and Arch based distros (Chakra, ArchBang, Parabola, ...)
#==============================================================================
#==============================================================================
# Copyright 2012 joe di castro <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#==============================================================================
__author__ = "joe di castro <[email protected]>"
__license__ = "GNU General Public License version 3"
__date__ = "2016-02-01"
__version__ = "0.3"
try:
import os
import platform
import sys
import time
from argparse import ArgumentParser
from difflib import unified_diff
from logger import Logger
from subprocess import Popen, PIPE
except ImportError:
# Checks the installation of the necessary python modules
print((os.linesep * 2).join(["An error found importing one module:",
str(sys.exc_info()[1]), "You need to install it", "Stopping..."]))
sys.exit(-2)
def arguments():
"""Defines the command line arguments for the script."""
desc = """Report changes in the packages installed on a Arch based sys"""
parser = ArgumentParser(description=desc)
parser.add_argument("path", default='./package_list.txt', nargs='?',
help="The path to store the arch packages list file")
parser.add_argument("-v", "--version", action="version",
version="%(prog)s {0}".format(__version__),
help="show program's version number and exit")
return parser
def main(old=""):
"""Main section"""
# The path to store the arch packages list file
args = arguments().parse_args()
pkg_lst_file = args.path
# Start logging
log = Logger()
url = "http://joedicastro.com"
head = "Changes of packages installed on {0}".format(platform.node())
log.header(url, head)
log.time("Start time")
# Read the old file and clean the list
if os.path.exists(pkg_lst_file):
old = open(pkg_lst_file, 'r').readlines()
old_date = time.ctime(os.stat(pkg_lst_file).st_mtime)
# Get the current list of arch packages installed on system (only the ones
# that were explicity installed)
official = Popen(["pacman", "-Qen"], stdout=PIPE).stdout.readlines()
aur = Popen(["pacman", "-Qem"], stdout=PIPE).stdout.readlines()
current = sorted(official + aur)
# First, save the list file
with open(pkg_lst_file, 'w') as out:
out.writelines(current)
curr_date = time.ctime(os.stat(pkg_lst_file).st_mtime)
# Compare both lists
if old:
file_path = os.path.realpath(pkg_lst_file)
diff = [ln for ln in unified_diff(old, current, fromfile="previous",
tofile="current ",
fromfiledate=old_date,
tofiledate=curr_date, n=0,
lineterm="")]
# If there are differences write the log to disk and send mail
if diff:
log.list("Installed packages list file", file_path)
log.list("Changes diff", diff)
log.time("End time")
log.write(True)
# Send mail to current system user. For other options, see logger
# module info
log.send("Arch packages changes")
if __name__ == "__main__":
main()