我正在开发一个自动化POC,其中我必须按顺序执行以下步骤:
1) 在工具的帮助下创建一个debian包,并将其推送到云存储库
2) 在我的prod服务器上,我将通过cron作业执行一个python程序,该程序将持续监视云位置,如果有任何新文件可用,它将从那里提取该文件并将其安装在python程序运行的服务器上(debian)。然而,云回购有可能在几天内无法获得新文件。因此,在这种情况下,即使repo在3天前更新,但是对于监视云位置的python程序来说,在新文件出现之前,上传到云上的文件将是最新的。因此,作为一个解决方案,我尝试使用逻辑来比较时间戳,即如果debian文件的时间戳没有改变,那么python程序应该退出/通过,或者执行业务逻辑。
我编写了两个程序,分别满足点(1)和部分满足点(2)。因此,接下来,我将重点讨论第(2)点代码。下面是我认为应该在后台工作以提取最新文件的代码,如果时间戳相同,那么它应该退出,为了测试场景,我刚刚获取了我的机器的本地路径:
import os
import subprocess
import glob
latest_file = 0 # initialized latest_file with zero to compare later
new_path = '/home/amitesh/Desktop'
file_path = glob.iglob('/home/amitesh/Desktop/linux_triad/*.deb')
latest_file = max(file_path, key=os.path.getctime) # Now the latest_file variable has a file in it
time_stamp = os.path.getmtime(latest_file)# gives the timestamp of the latest file
a = 0 # initialized it with zero to compare it with time stamp as follows.
while True:
if a == time_stamp:
pass
else:
subprocess.Popen(['cp', '-r', latest_file, new_path])
break
在上面的代码中,我只是尝试比较两个变量'a'和'timestamp',也就是说,如果a的值与time\u stamp相同,那么什么都不做,只传递。否则执行业务逻辑。
在执行代码时,我确实看到一个文件被复制到了所需的位置,但是,如果我检查文件的时间戳,它似乎不是最新的。下面是file\u path变量中可用的一组文件。
baqus_0.1-2_amd64.deb Tue 14 May 2019 01:24:02 PM IST
baqus_0.3-1_amd64.deb Tue 14 May 2019 01:24:04 PM IST
baqus_0.4-1_amd64.deb Tue 14 May 2019 01:24:09 PM IST
leesofd_0.1-1_amd64.deb Tue 14 May 2019 01:24:16 PM IST
syslmd_0.3-2_amd64.deb Tue 14 May 2019 01:24:21 PM IST
我的代码从列表中提取了第二个文件,如果仔细看,它似乎没有最新的时间戳。最新的时间戳属于syslmd\u 0.3-2\u amd64.deb,即IST下午01:24:21。所以,同样,我的代码是选择baqus\u 0.3-1\u amd64.deb,它的时间戳是01:24:04 PM,比它低。
为了确保我的观察结果是正确的,我已经执行了很多次了。每次将同一个文件复制到目标位置时。
所以,要么我的逻辑是错误的,要么我使用的函数可能是错误的。好心的建议。
if a == time_stamp:
pass
else:
print('copying of the ', latest_file, 'started')
sleep(4)
subprocess.Popen(['cp', '-r', latest_file, new_path])
sleep(3)
os.system('sudo dpkg --install ' +latest_file)
a = time_stamp