From 8d317ebed91fd20bd2d115e37a1914fa57feb352 Mon Sep 17 00:00:00 2001 From: wxy Date: Fri, 13 Apr 2018 23:18:58 +0800 Subject: [PATCH 01/15] PRF:20180111 AI and machine learning bias has dangerous implications.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit @Valoniakim 翻译的很好! --- ...earning bias has dangerous implications.md | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/translated/talk/20180111 AI and machine learning bias has dangerous implications.md b/translated/talk/20180111 AI and machine learning bias has dangerous implications.md index 3484b21163..ea6723b2f7 100644 --- a/translated/talk/20180111 AI and machine learning bias has dangerous implications.md +++ b/translated/talk/20180111 AI and machine learning bias has dangerous implications.md @@ -1,6 +1,8 @@ -AI 和机器中暗含的算法偏见是怎样形成的,我们又能通过开源社区做些什么 +AI 和机器学习中暗含的算法偏见 ====== +> 我们又能通过开源社区做些什么? + ![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/LAW_goodbadugly.png?itok=ZxaimUWU) 图片来源:opensource.com @@ -9,21 +11,21 @@ AI 和机器中暗含的算法偏见是怎样形成的,我们又能通过开 很难想像,我们经常忽略的一点是这二者的交集:计算机算法中存在的偏见。 -与我们大多数人所认为的相反,科技并不是客观的。 AI 算法和它们的决策程序是由它们的研发者塑造的,他们写入的代码,使用的“[训练][1]”数据还有他们对算法进行[应力测试][2] 的过程,都会影响这些算法今后的选择。这意味着研发者的价值观,偏见和人类缺陷都会反映在软件上。如果我只给实验室中的人脸识别算法提供白人的照片,当遇到不是白人照片时,它[不会认为照片中的是人类][3] 。这结论并不意味着 AI 是“愚蠢的”或是“天真的”,它显示的是训练数据的分布偏差:缺乏多种的脸部照片。这会引来非常严重的后果。 +与我们大多数人的认知相反,科技并不是客观的。 AI 算法和它们的决策程序是由它们的研发者塑造的,他们写入的代码,使用的“[训练][1]”数据还有他们对算法进行[应力测试][2] 的过程,都会影响这些算法今后的选择。这意味着研发者的价值观、偏见和人类缺陷都会反映在软件上。如果我只给实验室中的人脸识别算法提供白人的照片,当遇到不是白人照片时,它[不会认为照片中的是人类][3] 。这结论并不意味着 AI 是“愚蠢的”或是“天真的”,它显示的是训练数据的分布偏差:缺乏多种的脸部照片。这会引来非常严重的后果。 -这样的例子并不少。全美范围内的[州法院系统][4] 都使用“黑箱子”对罪犯进行宣判。由于训练数据的问题,[这些算法对黑人有偏见][5] ,他们对黑人罪犯会选择更长的服刑期,因此监狱中的种族差异会一直存在。而这些都发生在科技的客观性伪装下,这是“科学的”选择。 +这样的例子并不少。全美范围内的[州法院系统][4] 都使用“黑盒”对罪犯进行宣判。由于训练数据的问题,[这些算法对黑人有偏见][5] ,他们对黑人罪犯会选择更长的服刑期,因此监狱中的种族差异会一直存在。而这些都发生在科技的客观性伪装下,这是“科学的”选择。 美国联邦政府使用机器学习算法来计算福利性支出和各类政府补贴。[但这些算法中的信息][6],例如它们的创造者和训练信息,都很难找到。这增加了政府工作人员进行不平等补助金分发操作的几率。 -算法偏见情况还不止这些。从 Facebook 的新闻算法到医疗系统再到警方使用的相机,我们作为社会的一部分极有可能对这些算法输入各式各样的偏见,性别歧视,仇外思想,社会经济地位歧视,确认偏误等等。这些被输入了偏见的机器会大量生产分配,将种种社会偏见潜藏于科技客观性的面纱之下。 +算法偏见情况还不止这些。从 Facebook 的新闻算法到医疗系统再到警用携带相机,我们作为社会的一部分极有可能对这些算法输入各式各样的偏见、性别歧视、仇外思想、社会经济地位歧视、确认偏误等等。这些被输入了偏见的机器会大量生产分配,将种种社会偏见潜藏于科技客观性的面纱之下。 这种状况绝对不能再继续下去了。 在我们对人工智能进行不断开发研究的同时,需要降低它的开发速度,小心仔细地开发。算法偏见的危害已经足够大了。 -## 我们能怎样减少算法偏见? +### 我们能怎样减少算法偏见? -最好的方式是从算法训练的数据开始审查,根据 [Microsoft 的研究者][2] 所说,这方法很有效。 +最好的方式是从算法训练的数据开始审查,根据 [微软的研究人员][2] 所说,这方法很有效。 数据分布本身就带有一定的偏见性。编程者手中的美国公民数据分布并不均衡,本地居民的数据多于移民者,富人的数据多于穷人,这是极有可能出现的情况。这种数据的不平均会使 AI 对我们是社会组成得出错误的结论。例如机器学习算法仅仅通过统计分析,就得出“大多数美国人都是富有的白人”这个结论。 @@ -37,7 +39,7 @@ AI 和机器中暗含的算法偏见是怎样形成的,我们又能通过开 这些对于 AI 来说是十分复杂的数据,但我们可以通过多项测试对它们进行定义和传达。 -## 为什么开源很适合这项任务? +### 为什么开源很适合这项任务? 开源方法和开源技术都有着极大的潜力改变算法偏见。 @@ -45,17 +47,17 @@ AI 和机器中暗含的算法偏见是怎样形成的,我们又能通过开 调试工具如哥伦比亚大学和理海大学推出的 [DeepXplore][9],增强了 AI 应力测试的强度,同时提高了其操控性。还有 [麻省理工学院的计算机科学和人工智能实验室][10]完成的项目,它开发出敏捷快速的样机研究软件,这些应该会被开源社区采纳。 -开源技术也已经证明了其在审查和分类大组数据方面的能力。最明显的体现在开源工具在数据分析市场的占有率上(Weka , Rapid Miner 等等)。应当由开源社区来设计识别数据偏见的工具,已经在网上发布的大量训练数据组比如 [Kaggle][11]也应当使用这种技术进行识别筛选。 +开源技术也已经证明了其在审查和分类大组数据方面的能力。最明显的体现在开源工具在数据分析市场的占有率上(Weka、Rapid Miner 等等)。应当由开源社区来设计识别数据偏见的工具,已经在网上发布的大量训练数据组比如 [Kaggle][11] 也应当使用这种技术进行识别筛选。 -开源方法本身十分适合消除偏见程序的设计。内部谈话,私人软件开发及非民主的决策制定引起了很多问题。开源社区能够进行软件公开的谈话,进行大众化,维持好与大众的关系,这对于处理以上问题是十分重要的。如果线上社团,组织和院校能够接受这些开源特质,那么由开源社区进行消除算法偏见的机器设计也会顺利很多。 +开源方法本身十分适合消除偏见程序的设计。内部谈话、私人软件开发及非民主的决策制定引起了很多问题。开源社区能够进行软件公开的谈话,进行大众化,维持好与大众的关系,这对于处理以上问题是十分重要的。如果线上社团,组织和院校能够接受这些开源特质,那么由开源社区进行消除算法偏见的机器设计也会顺利很多。 -## 我们怎样才能够参与其中? +### 我们怎样才能够参与其中? -教育是一个很重要的环节。我们身边有很多还没意识到算法偏见的人,但算法偏见在立法,社会公正,政策及更多领域产生的影响与他们息息相关。让这些人知道算法偏见是怎样形成的和它们带来的重要影响是很重要的,因为想要改变目前是局面,从我们自身做起是唯一的方法。 +教育是一个很重要的环节。我们身边有很多还没意识到算法偏见的人,但算法偏见在立法、社会公正、政策及更多领域产生的影响与他们息息相关。让这些人知道算法偏见是怎样形成的和它们带来的重要影响是很重要的,因为想要改变目前的局面,从我们自身做起是唯一的方法。 -对于我们中间那些与人工智能一起工作的人来说,这种沟通尤其重要。不论是人工智能的研发者,警方或是科研人员,当他们为今后设计人工智能时,应当格外意识到现今这种偏见存在的危险性,很明显,想要消除人工智能中存在的偏见,就要从意识到偏见的存在开始。 +对于我们中间那些与人工智能一起工作的人来说,这种沟通尤其重要。不论是人工智能的研发者、警方或是科研人员,当他们为今后设计人工智能时,应当格外意识到现今这种偏见存在的危险性,很明显,想要消除人工智能中存在的偏见,就要从意识到偏见的存在开始。 -最后,我们需要围绕 AI 伦理化建立并加强开源社区。不论是需要建立应力实验训练模型,软件工具,或是从千兆字节的训练数据中筛选,现在已经到了我们利用开源方法来应对数字化时代最大的威胁的时间了。 +最后,我们需要围绕 AI 伦理化建立并加强开源社区。不论是需要建立应力实验训练模型、软件工具,或是从千兆字节的训练数据中筛选,现在已经到了我们利用开源方法来应对数字化时代最大的威胁的时间了。 -------------------------------------------------------------------------------- @@ -63,7 +65,7 @@ via: https://opensource.com/article/18/1/how-open-source-can-fight-algorithmic-b 作者:[Justin Sherman][a] 译者:[Valoniakim](https://github.com/Valoniakim) -校对:[校对者ID](https://github.com/校对者ID) +校对:[wxy](https://github.com/wxy) 本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 From 7f30faf26621318d121b0bf956a1c5a5b186e186 Mon Sep 17 00:00:00 2001 From: wxy Date: Fri, 13 Apr 2018 23:19:28 +0800 Subject: [PATCH 02/15] PUB: 20180111 AI and machine learning bias has dangerous implications.md @Valoniakim https://linux.cn/article-9543-1.html --- ...111 AI and machine learning bias has dangerous implications.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {translated/talk => published}/20180111 AI and machine learning bias has dangerous implications.md (100%) diff --git a/translated/talk/20180111 AI and machine learning bias has dangerous implications.md b/published/20180111 AI and machine learning bias has dangerous implications.md similarity index 100% rename from translated/talk/20180111 AI and machine learning bias has dangerous implications.md rename to published/20180111 AI and machine learning bias has dangerous implications.md From aad2bf79ef697b1adff23ab2051cbc5ada220796 Mon Sep 17 00:00:00 2001 From: MZqk Date: Sat, 14 Apr 2018 16:27:19 +0800 Subject: [PATCH 03/15] translating by MZqk --- sources/talk/20180214 11 awesome vi tips and tricks.md | 1 + 1 file changed, 1 insertion(+) diff --git a/sources/talk/20180214 11 awesome vi tips and tricks.md b/sources/talk/20180214 11 awesome vi tips and tricks.md index 8e75cf0503..6f1edc60d9 100644 --- a/sources/talk/20180214 11 awesome vi tips and tricks.md +++ b/sources/talk/20180214 11 awesome vi tips and tricks.md @@ -1,3 +1,4 @@ +translating by MZqk 11 awesome vi tips and tricks ====== From 38b66b6c988b1ce18be114683a4f196c1a426dff Mon Sep 17 00:00:00 2001 From: MZqk Date: Sat, 14 Apr 2018 16:28:26 +0800 Subject: [PATCH 04/15] translating by MZqk --- sources/talk/20180214 11 awesome vi tips and tricks.md | 1 + 1 file changed, 1 insertion(+) diff --git a/sources/talk/20180214 11 awesome vi tips and tricks.md b/sources/talk/20180214 11 awesome vi tips and tricks.md index 6f1edc60d9..da31cc8318 100644 --- a/sources/talk/20180214 11 awesome vi tips and tricks.md +++ b/sources/talk/20180214 11 awesome vi tips and tricks.md @@ -1,4 +1,5 @@ translating by MZqk + 11 awesome vi tips and tricks ====== From 0ddb39578e81a1c433c000189b0d1f686dbc672d Mon Sep 17 00:00:00 2001 From: wxy Date: Sat, 14 Apr 2018 22:29:13 +0800 Subject: [PATCH 05/15] PRF:20180313 Running DOS on the Raspberry Pi.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit @HandChow 翻译不错! --- ...0180313 Running DOS on the Raspberry Pi.md | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/translated/tech/20180313 Running DOS on the Raspberry Pi.md b/translated/tech/20180313 Running DOS on the Raspberry Pi.md index 688927d918..94d380f534 100644 --- a/translated/tech/20180313 Running DOS on the Raspberry Pi.md +++ b/translated/tech/20180313 Running DOS on the Raspberry Pi.md @@ -1,7 +1,10 @@ 在树莓派上运行 DOS 系统 ====== +> 不同的 CPU 架构意味着在树莓派上运行 DOS 并非唾手可得,但其实也没多麻烦。 + ![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/wings_freedos_game.jpg?itok=7j8x-A-w) + [FreeDOS][1] 对大家来说也许并不陌生。它是一个完整、免费并且对 DOS 兼容良好的操作系统,它可以运行一些比较老旧的 DOS 游戏或者商用软件,也可以开发嵌入式的应用。只要在 MS-DOS 上能够运行的程序,在 FreeDOS 上都可以运行。 作为 FreeDOS 的发起者和项目协调人员,很多用户会把我作为内行人士进行发问。而我最常被问到的问题是:“FreeDOS 可以在树莓派上运行吗?” @@ -12,57 +15,54 @@ 不过通过 PC 模拟器还是能在树莓派上运行 FreeDOS 的,虽然这样也许稍有不足,但也不失为一个能在树莓派上运行 FreeDOS 的方法。 -### DOSBox怎么样? +### DOSBox 怎么样? -有人可能会问:“为什么不用 DOSBox 呢?” DOSBox 是一个开源的跨平台 x86 模拟器,在 Linux 上也能使用,它能够为应用软件尤其是游戏软件提供了一个类 DOS 的运行环境,所以如果你只是想玩 DOS 游戏的话,DOSBox是一个不错的选择。但在大众眼中,DOSBox 是专为 DOS 游戏而设的,而在运行一些别的 DOS 应用软件方面,DOSBox 只是表现平平。 +有人可能会问:“为什么不用 DOSBox 呢?” DOSBox 是一个开源的跨平台 x86 模拟器,在 Linux 上也能使用,它能够为应用软件尤其是游戏软件提供了一个类 DOS 的运行环境,所以如果你只是想玩 DOS 游戏的话,DOSBox 是一个不错的选择。但在大众眼中,DOSBox 是专为 DOS 游戏而设的,而在运行一些别的 DOS 应用软件方面,DOSBox 只是表现平平。 对多数人来说,这只是个人偏好的问题,我喜欢用 FreeDOS 来运行 DOS 游戏和其它程序,完整的 DOS 系统和 DOSBox 相比能让我体验到更好的灵活性和操控性。我只用 DOSBox 来玩游戏,在其它方面还是选择完整的 FreeDOS。 ### 在树莓派上安装 FreeDOS -[QEMU][3](Quick EMUlator)是一款能在 Linux 系统上运行 DOS 系统的开源的虚拟机软件。很多流行的 Linux 系统都自带 QEMU。QEMU 在我树莓派上的 Raspbian 系统中也同样能够运行,下文就有一些我在树莓派 [Raspbian GNU/Linux 9 (Stretch)][4] 系统中使用 QEMU 的截图。 +[QEMU][3](Quick EMUlator)是一款能在 Linux 系统上运行 DOS 系统的开源的虚拟机软件。很多流行的 Linux 系统都自带 QEMU。QEMU 在我的树莓派上的 Raspbian 系统中也同样能够运行,下文就有一些我在树莓派 [Raspbian GNU/Linux 9 (Stretch)][4] 系统中使用 QEMU 的截图。 去年我在写了一篇关于[如何在 Linux 系统中运行 DOS 程序][5]的文章的时候就用到了 QEMU,在树莓派上使用 QEMU 来安装运行 FreeDOS 的步骤基本上和在别的基于 GNOME 的系统上没有什么太大的区别。 -在 QEMU 中你需要通过添加各种组件来搭建虚拟机。先指定一个用来安装运行 DOS 的虚拟磁盘镜像,通过`qemu-img` 命令来创建一个虚拟磁盘镜像,对于 FreeDOS 来说不需要太大的空间,所以我只创建了一个 200MB 的虚拟磁盘: +在 QEMU 中你需要通过添加各种组件来搭建虚拟机。先指定一个用来安装运行 DOS 的虚拟磁盘镜像,通过 `qemu-img` 命令来创建一个虚拟磁盘镜像,对于 FreeDOS 来说不需要太大的空间,所以我只创建了一个 200MB 的虚拟磁盘: + ``` qemu-img create freedos.img 200M - ``` 和 VMware 或者 VirtualBox 这些 PC 模拟器不同,使用 QEMU 需要通过添加各种组件来搭建虚拟机,尽管有点麻烦,但是并不困难。我使用了以下这些参数来在树莓派上使用 QEMU 安装 FreeDOS 系统: + ``` qemu-system-i386 -m 16 -k en-us -rtc base=localtime -soundhw sb16,adlib -device cirrus-vga -hda freedos.img -cdrom FD12CD.iso -boot order=d - ``` -你可以在我其它的[文章][5]中找到这些命令的完整介绍。简单来说,上面这条命令指定了一个英特尔 i386 兼容虚拟机,并且分配了 16MB 内存、一个英文输入键盘、一个基于系统时间的实时时钟、一个声卡、一个音乐卡以及一个 VGA 卡。文件 `freedos.img` 指定为第一个硬盘(`C:`),`FD12CD.iso` 镜像作为 CD-ROM (`D:`)驱动。QEMU 设定为从`D:`的 CD-ROM 启动。 +你可以在我其它的[文章][5]中找到这些命令的完整介绍。简单来说,上面这条命令指定了一个英特尔 i386 兼容虚拟机,并且分配了 16MB 内存、一个英文输入键盘、一个基于系统时间的实时时钟、一个声卡、一个音乐卡以及一个 VGA 卡。文件 `freedos.img` 指定为第一个硬盘(`C:`),`FD12CD.iso` 镜像作为 CD-ROM (`D:`)驱动。QEMU 设定为从 `D:` 的 CD-ROM 启动。 你只需要按照提示就可以轻松安装好 FreeDOS 1.2 了。但是由于 microSD 卡在面对大量的 I/O 时速度比较慢,所以安装操作系统需要花费很长时间。 - ### 在树莓派上运行 FreeDOS 你的运行情况取决于使用哪一种 microSD 卡。我用的是 SanDisk Ultra 64GB microSDXC UHS-I U1A1 ,其中 U1 这种型号专用于支持 1080p 的视频录制(例如 GoPro),它的最低串行写速度能够达到 10MB/s。相比之下,V60 型号专用于 4K 视频录制,最低连续写入速度能达到 60MB/s。如果你的树莓派使用的是 V60 的 microSD 卡甚至是 V30(也能达到 30MB/s),你就能明显看到它的 I/O 性能会比我的好。 -FreeDOS 安装好之后,你可以直接从`C:`进行启动。只需要按照下面的命令用`-boot order=c`来指定 QEMU 的启动顺序即可: +FreeDOS 安装好之后,你可以直接从 `C:` 进行启动。只需要按照下面的命令用 `-boot order=c` 来指定 QEMU 的启动顺序即可: + ``` ​qemu-system-i386 -m 16 -k en-us -rtc base=localtime -soundhw sb16,adlib -device cirrus-vga -hda freedos.img -cdrom FD12CD.iso -boot order=c​ - ``` 只要树莓派的 QEMU 上安装了 FreeDOS,就不会出现明显的性能问题。例如游戏通常在每一关开始的时候会加载地图、怪物、声音等一系列的数据,尽管这些内容需要加载一段时间,但在正常玩的时候并没有出现性能不足的现象。 -FreeDOS 1.2 自带了很多游戏以及其它应用软件,可以使用`FDIMPLES`包管理程序来安装它们。FreeDOS 1.2 里面我最喜欢的是一款叫 WING 的太空射击游戏,让人想起经典的街机游戏 Galaga(WING 就是 Wing Is Not Galaga 的递归缩写词)。 +FreeDOS 1.2 自带了很多游戏以及其它应用软件,可以使用 `FDIMPLES` 包管理程序来安装它们。FreeDOS 1.2 里面我最喜欢的是一款叫 WING 的太空射击游戏,让人想起经典的街机游戏 Galaga(WING 就是 Wing Is Not Galaga 的递归缩写词)。 -As-Easy-As 是我最喜欢的一个 DOS 应用程序,作为20世纪8、90年代流行的电子表格程序,它和当时的 Lotus 1-2-3 以及现在的 Microsoft Excel、LibreOffice Calc 一样具有强大的威力。As-Easy-As 和 Lotus 1-2-3 都将数据保存为 WKS 文件,现在新版本的 Microsoft Excel 已经无法读取这种文件了,而 LibreOffice Calc 是兼容性而定有可能支持。鉴于 As-Easy-As 的初始版本是一个共享软件,TRIUS 仍然为 As-Easy-As 5.7 免费提供[激活码][6]。 +As-Easy-As 是我最喜欢的一个 DOS 应用程序,作为 20 世纪八九十年代流行的电子表格程序,它和当时的 Lotus 1-2-3 以及现在的 Microsoft Excel、LibreOffice Calc 一样具有强大的威力。As-Easy-As 和 Lotus 1-2-3 都将数据保存为 WKS 文件,现在新版本的 Microsoft Excel 已经无法读取这种文件了,而 LibreOffice Calc 视兼容性而定有可能支持。鉴于 As-Easy-As 的初始版本是一个共享软件,TRIUS 仍然为 As-Easy-As 5.7 免费提供[激活码][6]。 我也非常喜欢 GNU Emacs 编辑器,FreeDOS 也自带了一个叫 Freemacs 的类 Emacs 的文本编辑器。它比 FreeDOS 默认的 FreeDOS Edit 编辑器更强大,也能带来 GNU Emacs 的体验。如果你也需要,可以在 FreeDOS 1.2 中通过`FDIMPLES`包管理程序来安装。 ### 是的,你或许真的可以在树莓派上运行 DOS -While you can't run DOS on "bare hardware" on the Raspberry Pi, it's nice to know that you can still run DOS on the Raspberry Pi via an emulator. Thanks to the QEMU PC emulator and FreeDOS, it's possible to play classic DOS games and run other DOS programs on the Raspberry Pi. Expect a slight performance hit when doing any disk I/O, especially if you're doing something intensive on the disk, like writing large amounts of data, but things will run fine after that. Once you've set up QEMU as the virtual machine emulator and installed FreeDOS, you are all set to enjoy your favorite classic DOS programs on the Raspberry Pi. - 即使树莓派在硬件上不支持 DOS,但是在模拟器的帮助下,DOS 还是能够在树莓派上运行。得益于 QEMU PC 模拟器,一些经典的 DOS 游戏和 DOS 应用程序能够运行在树莓派上。在执行磁盘 I/O ,尤其是大量密集操作(例如写入大量数据)的时候,性能可能会受到轻微的影响。当你使用 QEMU 并且在虚拟机里安装好 FreeDOS 之后,你就可以尽情享受经典的 DOS 程序了。 -------------------------------------------------------------------------------- @@ -71,7 +71,7 @@ via: https://opensource.com/article/18/3/can-you-run-dos-raspberry-pi 作者:[Jim Hall][a] 译者:[HankChow](https://github.com/HankChow) -校对:[校对者ID](https://github.com/校对者ID) +校对:[wxy](https://github.com/wxy) 本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 @@ -80,5 +80,5 @@ via: https://opensource.com/article/18/3/can-you-run-dos-raspberry-pi [2]:https://opensource.com/article/18/3/raspberry-pi-week-giveaway [3]:https://www.qemu.org/ [4]:https://www.raspberrypi.org/downloads/ -[5]:https://opensource.com/article/17/10/run-dos-applications-linux +[5]:https://linux.cn/article-9014-1.html [6]:http://www.triusinc.com/forums/viewtopic.php?t=10 From 436cc2d88eb89e75e582d71aab3200e36b92202c Mon Sep 17 00:00:00 2001 From: wxy Date: Sat, 14 Apr 2018 22:29:48 +0800 Subject: [PATCH 06/15] PUB:20180313 Running DOS on the Raspberry Pi.md @HankChow https://linux.cn/article-9544-1.html --- .../20180313 Running DOS on the Raspberry Pi.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {translated/tech => published}/20180313 Running DOS on the Raspberry Pi.md (100%) diff --git a/translated/tech/20180313 Running DOS on the Raspberry Pi.md b/published/20180313 Running DOS on the Raspberry Pi.md similarity index 100% rename from translated/tech/20180313 Running DOS on the Raspberry Pi.md rename to published/20180313 Running DOS on the Raspberry Pi.md From afcd8c6955958467e2b52b90f741790591dfacc5 Mon Sep 17 00:00:00 2001 From: wxy Date: Sun, 15 Apr 2018 08:49:47 +0800 Subject: [PATCH 07/15] PRF:20180220 How to Get Started Using WSL in Windows 10.md @CYLeft --- ... to Get Started Using WSL in Windows 10.md | 59 +++++++++---------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/translated/tech/20180220 How to Get Started Using WSL in Windows 10.md b/translated/tech/20180220 How to Get Started Using WSL in Windows 10.md index 5816c6fdfd..68f507399f 100644 --- a/translated/tech/20180220 How to Get Started Using WSL in Windows 10.md +++ b/translated/tech/20180220 How to Get Started Using WSL in Windows 10.md @@ -1,9 +1,11 @@ -如何在 Windows 10 上开启 WSL(Windows Subsystem for Linux) 之旅 +如何在 Windows 10 上开启 WSL 之旅 ====== +> WSL 可以让你访问 Windows 上的 Linux Bash shell。 + ![](https://www.linux.com/sites/lcom/files/styles/rendered_file/public/wsl-main.png?itok=wJ5WrU9U) -在 [上一篇文章][1] 中,我们讨论过关于 Windows 的子系统 Linux(WSL)的目标用户。本文,我们将在 Windows 10 的设备上,开启 WSL 的旅程。 +在 [上一篇文章][1] 中,我们讨论过关于 Windows 的子系统 LinuxWindows Subsystem for Linux(WSL)的目标用户。本文,我们将在 Windows 10 的设备上,开启 WSL 的旅程。 ### 为 WSL 做准备 @@ -11,85 +13,82 @@ 这里有一张关于我的操作系统的截图。 -![kHFKOvrbG1gXdB9lsbTqXC4N4w0Lbsz1Bul5ey9m][2] +![][2] -如果您安装了之前的版本,您有必要在 [这里][3] 下载并且安装 Windows 10 Fall Creator Update (FCU)。安装完毕后,更新设置(在开始菜单的搜索框中搜索 “updates”)。 +如果您安装了之前的版本,您需要在 [这里][3] 下载并且安装 Windows 10 Fall Creator Update (FCU)。安装完毕后,安装可用的更新(在开始菜单的搜索框中搜索 “updates”)。 -前往 `启用或关闭 Windows 功能` 页面,然后滚动至底部,如截图所示,勾选 `适用于 Linux 的 Windows 子系统`,点击确定。它将会下载安装需要的包。 +前往 “启用或关闭 Windows 功能” ,然后滚动至底部,如截图所示,勾选 “适用于 Linux 的 Windows 子系统”,点击确定。它将会下载安装需要的包。 -![oV1mDqGe3zwQgL0N3rDasHH6ZwHtxaHlyrLzjw7x][4] +![][4] 安装完成之后,系统将会询问是否重启。是的,重启设备吧。WSL 在系统重启之前不会启动,如下所示: -![GsNOQLJlHeZbkaCsrDIhfVvEoycu3D0upoTdt6aN][5] +![][5] -一旦您的系统重启,返回 `启用或关闭 Windows 功能` 页面,确认 `适用于 Linux 的 Windows 子系统` 已经被勾选。 +一旦您的系统重启,返回 “启用或关闭 Windows 功能” 页面,确认 “适用于 Linux 的 Windows 子系统” 已经被勾选。 ### 在 Windows 中安装 Linux 在 Windows 中安装 Linux,有很多方式,这里我们选择一种最简单的方式。打开 Microsoft Store,搜索 Linux。您将看到下面的选项: -![YAR4UgZiFAy2cdkG4U7jQ7_m81lrxR6aHSMOdED7][6] +![][6] -点击 `获取`,之后 Windows 商店将会提供三个选项:Ubuntu,openSUSE Leap 42 和 SUSE Linux Enterprise Server。您可以一并安装上述三个发行版,并且它们可以同时运行。为了能使用 SLE,您需要订阅消息。 +点击 “获取”,之后 Windows 商店将会提供三个选项:Ubuntu、openSUSE Leap 42 和 SUSE Linux Enterprise Server。您可以一并安装上述三个发行版,并且它们可以同时运行。为了能使用 SLE,您需要一份订阅。 -在此,我将安装 openSUSE Leap 42 和 Ubuntu。选中您想要的发行版,点击获得按钮并安装。一旦安装完毕,您就可以在 Windows 中启动 openSUSE。为了方便访问,可以将其固定到开始菜单中。 +在此,我将安装 openSUSE Leap 42 和 Ubuntu。选中您想要的发行版,点击“获得”按钮并安装。一旦安装完毕,您就可以在 Windows 中启动 openSUSE。为了方便访问,可以将其固定到开始菜单中。 -![4LU6eRrzDgBprDuEbSFizRuP1J_zS3rBnoJbU2OA][7] +![][7] ### 在 Windwods 中使用 Linux -当您启动发行版,它将会打开一个 Bash Shell 并且安装此发行版。安装完毕之后,您就可以开始使用了。您需要留意,openSUSE 中并没有用户,它直接运行在 root 用户下,但是 Ubuntu 会询问您是否创建用户。在 Ubuntu,您可以执行 sudo 用户管理任务。 +当您启动该发行版,它将会打开一个 Bash Shell 并且安装此发行版。安装完毕之后,您就可以开始使用了。您需要留意,openSUSE 中并没有(普通)用户,它直接运行在 `root` 用户下,但是 Ubuntu 会询问您是否创建用户。在 Ubuntu,您可以以 `sudo` 用户执行管理任务。 在 openSUSE 上,您可以很轻松的创建一个用户: + ``` # useradd [username] - # passwd [username] - ``` 为此用户创建一个新的密码。例如: + ``` # useradd swapnil - # passwd swapnil - ``` -您可以通过 su 命令从 root 用户切换过来。 +您可以通过 `su` 命令从 root 用户切换过来。 + ``` su swapnil - ``` -您需要非根用户来执行许多任务,比如使用 rsync 移动文件到本地设备。 +您需要非根用户来执行许多任务,比如使用 `rsync` 移动文件到本地设备。 而首要任务是更新发行版。对于 openSUSE 来说,您应该: + ``` zypper up - ``` 而对于 Ubuntu: + ``` sudo apt-get update - sudo apt-get dist-upgrade - ``` -![7cRgj1O6J8yfO3L4ol5sP-ZCU7_uwOuEoTzsuVW9][8] +![][8] -现在,您就在 Windows 上拥有了原生 Linux Bash shell。想在 Windows 10 上通过 ssh 连接您的服务器?不需要安装 puTTY 或是 Cygwin。打开 Bash 之后,就可以通过 ssh 进入您的服务器。简单之至。 +现在,您就在 Windows 上拥有了原生的 Linux Bash shell。想在 Windows 10 上通过 `ssh` 连接您的服务器?不需要安装 puTTY 或是 Cygwin。打开 Bash 之后,就可以通过 `ssh` 进入您的服务器。简单之至。 -想通过 rsync 同步文件到您的服务器?直接使用 rsync。它切实的将我们的 Windows 设备转变得更为实用,帮助那些需要使用原生 Linux 命令和 Linux 工具的用户避开虚拟机,大开方便之门。 +想通过 `rsync` 同步文件到您的服务器?直接使用 `rsync`。它切实的将我们的 Windows 设备转变得更为实用,帮助那些需要使用原生 Linux 命令和 Linux 工具的用户避开虚拟机,大开方便之门。 -### 找不到 Fedora? +### Fedora 在哪里? -您可能想了解 Fedora。可惜,商城里并没有 Fedora。Fedora 项目发布负责人在 Twitter 上表示,“我们正在解决一些非技术性问题。现在可能提供不了更多了。” +您可能奇怪为什么没有 Fedora。可惜,商城里并没有 Fedora。Fedora 项目发布负责人在 Twitter 上表示,“我们正在解决一些非技术性问题。现在可能提供不了更多了。” -我们并不确定这些非技术性问题是什么。当一些用户询问 WSL 团队为何不发布 Fedora,毕竟它也是一个开源项目。项目负责人 Rich Turner 在 Microsoft [回应][9],“我们没有发布其他 IP 到应用商店的政策。我们相信,相较于被微软或是其他非权威人士,社区更希望看到发行版由发行版所有者发布。” +我们并不确定这些非技术性问题是什么。当一些用户询问 WSL 团队为何不发布 Fedora,毕竟它也是一个开源项目。项目负责人 Rich Turner 在 Microsoft [回应][9],“我们有一个不发布其他知识产权到应用商店的政策。我们相信,相较于被微软或是其他非权威人士,社区更希望看到发行版由发行版所有者发布。” 因此,微软不方便在 Windows 商店中直接发布 Debian 或是 Arch 系统。这些任务应该落在他们的官方团队中,应该由他们将发行版带给 Windows 10 的用户。 @@ -103,7 +102,7 @@ via: https://www.linux.com/blog/learn/2018/2/how-get-started-using-wsl-windows-1 作者:[SWAPNIL BHARTIYA][a] 译者:[CYLeft](https://github.com/CYLeft) -校对:[校对者ID](https://github.com/校对者ID) +校对:[wxy](https://github.com/wxy) 本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 From 27c5b5170e121e931d92413f6d85efee93bb9539 Mon Sep 17 00:00:00 2001 From: wxy Date: Sun, 15 Apr 2018 08:50:23 +0800 Subject: [PATCH 08/15] PUB:20180220 How to Get Started Using WSL in Windows 10.md @CYLeft https://linux.cn/article-9545-1.html --- .../20180220 How to Get Started Using WSL in Windows 10.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {translated/tech => published}/20180220 How to Get Started Using WSL in Windows 10.md (100%) diff --git a/translated/tech/20180220 How to Get Started Using WSL in Windows 10.md b/published/20180220 How to Get Started Using WSL in Windows 10.md similarity index 100% rename from translated/tech/20180220 How to Get Started Using WSL in Windows 10.md rename to published/20180220 How to Get Started Using WSL in Windows 10.md From a068c78eb2593459d25de30d3749034383a60419 Mon Sep 17 00:00:00 2001 From: wxy Date: Sun, 15 Apr 2018 09:32:27 +0800 Subject: [PATCH 09/15] PRF:20180206 Programming in Color with ncurses.md @leemeans --- ...80206 Programming in Color with ncurses.md | 98 ++++++++----------- 1 file changed, 42 insertions(+), 56 deletions(-) diff --git a/translated/tech/20180206 Programming in Color with ncurses.md b/translated/tech/20180206 Programming in Color with ncurses.md index 18960de398..b2ab9d273f 100755 --- a/translated/tech/20180206 Programming in Color with ncurses.md +++ b/translated/tech/20180206 Programming in Color with ncurses.md @@ -1,103 +1,92 @@ -使用ncurses进行颜色编程 +使用 ncurses 进行颜色编程 ====== -在我的使用ncurses库进行编程的系列文章的[第一篇][1]和[第二篇][2]中,我已经介绍了一些curses的函数来在屏幕上作画、从屏幕上查询和从键盘读取字符。为了搞清楚这些函数,我使用curses来利用简单字符绘制游戏地图和玩家字符创建了一个简单的冒险游戏。在这篇紧接着的文章里,我展示了如何为你的curses程序添加颜色。 -在屏幕上绘图一切都挺好的,但是如果只有黑底白字的文本,你的程序可能看起来很无趣。颜色可以帮助传递更多的信息。举个例子,如果你的程序需要报告(执行)成功或者(执行)失败时。在这样的情况下你可以使用绿色或者红色来帮助强调输出。或者,你只是简单地想要“潮艺”一下给你的程序来让它看起来更美观。 +![](https://www.linuxjournal.com/sites/default/files/styles/850x500/public/nodeimage/story/quest-color-1.png?itok=lOK4ldl6) -在这篇文章中,我用一个简单的例子来展示通过curses函数进行颜色操作。在我先前的文章中,我写了一个允许你在一个粗糙绘制的地图上移动玩家字符的初级冒险类游戏。但是那里面的地图完全是白色和黑色的文本,通过形状来表明是水(~)或者山(^)。所以,让我们将游戏更新到使用颜色(的版本)吧。 +> Jim 给他的终端冒险游戏添加了颜色,演示了如何用 curses 操纵颜色。 + +在我的使用 ncurses 库进行编程的系列文章的[第一篇][1]和[第二篇][2]中,我已经介绍了一些 curses 函数来在屏幕上作画、从屏幕上查询和从键盘读取字符。为了搞清楚这些函数,我使用 curses 来利用简单字符绘制游戏地图和玩家角色,创建了一个简单的冒险游戏。在这篇紧接着的文章里,我展示了如何为你的 curses 程序添加颜色。 + +在屏幕上绘图一切都挺好的,但是如果只有黑底白字的文本,你的程序可能看起来很无趣。颜色可以帮助传递更多的信息。举个例子,如果你的程序需要报告执行成功或者执行失败时。在这样的情况下你可以使用绿色或者红色来帮助强调输出。或者,你只是简单地想要“潮艺”一下给你的程序来让它看起来更美观。 + +在这篇文章中,我用一个简单的例子来展示通过 curses 函数进行颜色操作。在我先前的文章中,我写了一个可以让你在一个粗糙绘制的地图上移动玩家角色的初级冒险类游戏。但是那里面的地图完全是白色和黑色的文本,通过形状来表明是水(`~`)或者山(`^`)。所以,让我们将游戏更新到使用颜色的版本吧。 ### 颜色要素 -在你可以使用颜色之前,你的程序得要知道它是否可以依靠终端正确地显示颜色。在现代操作系统上,此处应该永远为true。但是在经典的计算机上,一些终端是单色的,例如令人尊敬的VT52和VT100终端,一般(它们)提供黑底白色或者黑底绿色的文本。 +在你可以使用颜色之前,你的程序需要知道它是否可以依靠终端正确地显示颜色。在现代操作系统上,此处应该永远为true。但是在经典的计算机上,一些终端是单色的,例如古老的 VT52 和 VT100 终端,一般它们提供黑底白色或者黑底绿色的文本。 -可以使用has_colors()函数查询终端的颜色功能。这个函数将会在终端可以显示颜色的时候返回true,否则将会返回false。这个函数一般用于if块的开头,就像这样: +可以使用 `has_colors()` 函数查询终端的颜色功能。这个函数将会在终端可以显示颜色的时候返回 `true`,否则将会返回 `false`。这个函数一般用于 `if` 块的开头,就像这样: ``` - if (has_colors() == FALSE) { endwin(); printf("Your terminal does not support color\n"); exit(1); } - ``` -在知道终端可以显示颜色之后,你可以使用start_color()函数来设置curses使用颜色。现在是时候定义程序将要使用的颜色了。 +在知道终端可以显示颜色之后,你可以使用 `start_color()` 函数来设置 curses 使用颜色。现在是时候定义程序将要使用的颜色了。 -在curses中,你应该按对定义颜色:一个前景色放在一个背景色上。这样允许curses一次性设置两个颜色属性,这也是一般你想要使用的方式。通过init_pair()函数可以定义一个前景色和背景色并关联到索引数字来设置颜色对。大致语法如下: +在 curses 中,你应该按对定义颜色:一个前景色放在一个背景色上。这样允许 curses 一次性设置两个颜色属性,这也是一般你想要使用的方式。通过 `init_pair()` 函数可以定义一个前景色和背景色并关联到索引数字来设置颜色对。大致语法如下: ``` - init_pair(index, foreground, background); - ``` 控制台支持八种基础的颜色:黑色、红色、绿色、黄色、蓝色、品红色、青色和白色。这些颜色通过下面的名称为你定义好了: -* COLOR_BLACK - -* COLOR_RED - -* COLOR_GREEN - -* COLOR_YELLOW - -* COLOR_BLUE - -* COLOR_MAGENTA - -* COLOR_CYAN - -* COLOR_WHITE +* `COLOR_BLACK` +* `COLOR_RED` +* `COLOR_GREEN` +* `COLOR_YELLOW` +* `COLOR_BLUE` +* `COLOR_MAGENTA` +* `COLOR_CYAN` +* `COLOR_WHITE` ### 应用颜色 -在我的冒险游戏中,我想要让草地呈现绿色而玩家的足迹变成不易察觉的绿底黄色点迹。水应该是蓝色,那些(表示波浪的)腭化符号应该是近似青色的。我想让山是灰色的,但是我可以用白底黑色文本做一个可用的折中方案。(译注:意为终端预设的颜色没有灰色,使用白底黑色文本做一个折中方案)为了让玩家的字符更易见,我想要使用一个刺目的品红底红色设计。我可以像这样定义这些颜色对: +在我的冒险游戏中,我想要让草地呈现绿色而玩家的足迹变成不易察觉的绿底黄色点迹。水应该是蓝色,那些表示波浪的 `~` 符号应该是近似青色的。我想让山(`^`)是灰色的,但是我可以用白底黑色文本做一个可用的折中方案。(LCTT 译注:意为终端预设的颜色没有灰色,使用白底黑色文本做一个折中方案)为了让玩家的角色更易见,我想要使用一个刺目的品红底红色设计。我可以像这样定义这些颜色对: ``` - start_color(); init_pair(1, COLOR_YELLOW, COLOR_GREEN); init_pair(2, COLOR_CYAN, COLOR_BLUE); init_pair(3, COLOR_BLACK, COLOR_WHITE); init_pair(4, COLOR_RED, COLOR_MAGENTA); - ``` 为了让颜色对更容易记忆,我的程序中定义了一些符号常量: -``` +``` #define GRASS_PAIR 1 #define EMPTY_PAIR 1 #define WATER_PAIR 2 #define MOUNTAIN_PAIR 3 #define PLAYER_PAIR 4 - ``` 有了这些常量,我的颜色定义就变成了: -``` +``` start_color(); init_pair(GRASS_PAIR, COLOR_YELLOW, COLOR_GREEN); init_pair(WATER_PAIR, COLOR_CYAN, COLOR_BLUE); init_pair(MOUNTAIN_PAIR, COLOR_BLACK, COLOR_WHITE); init_pair(PLAYER_PAIR, COLOR_RED, COLOR_MAGENTA); - ``` -在任何时候你想要使用颜色显示文本,你只需要告诉curses设置那种颜色属性。为了更好的编程实践,你同样应该在你完成了颜色使用的时候告诉curses取消颜色组合。为了设置颜色,应该在调用像mvaddch()这样的函数之前使用attron(),然后通过attroff()关闭颜色属性。例如,在我绘制玩家的字符的时候,我应该这样做: -``` +在任何时候你想要使用颜色显示文本,你只需要告诉 curses 设置哪种颜色属性。为了更好的编程实践,你同样应该在你完成了颜色使用的时候告诉 curses 取消颜色组合。为了设置颜色,应该在调用像 `mvaddch()` 这样的函数之前使用`attron()`,然后通过 `attroff()` 关闭颜色属性。例如,在我绘制玩家角色的时候,我应该这样做: +``` attron(COLOR_PAIR(PLAYER_PAIR)); mvaddch(y, x, PLAYER); attroff(COLOR_PAIR(PLAYER_PAIR)); - ``` -记住将颜色应用到你的程序添加了不可见的改变到你如何查询屏幕。一般来讲,由mvinch()函数返回的值是**没有**带颜色属性的类型chtype,这个值基本上是一个整型值也可以就当作整型值来用。但是,由于(使用)颜色添加了额外的属性到屏幕上的字符上,所以chtype按照扩展的位模式携带了额外的颜色信息。一旦你使用mvinch(),返回值将会包含这些额外的颜色值。为了只提取**文本**值,例如在is_move_okay()函数中,你需要和A_CHARTEXT做&位运算: -``` +记住将颜色应用到你的程序对你如何查询屏幕有一些微妙的影响。一般来讲,由 `mvinch()` 函数返回的值是**没有**带颜色属性的类型 `chtype`,这个值基本上是一个整型值,也可以当作整型值来用。但是,由于使用颜色添加了额外的属性到屏幕上的字符上,所以 `chtype` 按照扩展的位模式携带了额外的颜色信息。一旦你使用 `mvinch()`,返回值将会包含这些额外的颜色值。为了只提取**文本**值,例如在 `is_move_okay()` 函数中,你需要和 `A_CHARTEXT` 做 `&` 位运算: +``` int is_move_okay(int y, int x) { int testch; @@ -108,16 +97,15 @@ int is_move_okay(int y, int x) return (((testch & A_CHARTEXT) == GRASS) || ((testch & A_CHARTEXT) == EMPTY)); } - ``` 通过这些修改,我可以用颜色更新这个冒险游戏: -``` +``` /* quest.c */ -#include -#include +#include +#include #define GRASS ' ' #define EMPTY '.' @@ -280,12 +268,11 @@ void draw_map(void) } attroff(COLOR_PAIR(WATER_PAIR)); } - ``` -你可能不能认出所有为了在冒险游戏里面支持颜色需要的修改,除非你目光敏锐。diff工具展示了所有为了支持颜色而添加的函数或者修改的代码: -``` +你可能不能认出所有为了在冒险游戏里面支持颜色需要的修改,除非你目光敏锐。`diff` 工具展示了所有为了支持颜色而添加的函数或者修改的代码: +``` $ diff quest-color/quest.c quest/quest.c 12,17d11 < #define GRASS_PAIR 1 @@ -350,28 +337,27 @@ $ diff quest-color/quest.c quest/quest.c < attron(COLOR_PAIR(WATER_PAIR)); 164d125 < attroff(COLOR_PAIR(WATER_PAIR)); - ``` ### 开始玩吧--现在有颜色了 -程序现在有了更舒服的颜色设计了,更匹配起初的桌游地图,有绿地、蓝色的湖和壮观的灰色山峰。英雄穿着红色的制服十分夺目。 +程序现在有了更舒服的颜色设计了,更匹配原来的桌游地图,有绿色的地、蓝色的湖和壮观的灰色山峰。英雄穿着红色的制服十分夺目。 ![](http://www.linuxjournal.com/files/linuxjournal.com/ufiles/imagecache/large-550px-centered/u1000009/quest-map_0.jpg) -图 1\. 一个简单的带湖和山的桌游地图 +*图 1. 一个简单的带湖和山的桌游地图* ![](http://www.linuxjournal.com/files/linuxjournal.com/ufiles/imagecache/large-550px-centered/u1000009/quest-color-start.png) -图 2\.玩家站在左下角 +*图 2. 玩家站在左下角* ![](http://www.linuxjournal.com/files/linuxjournal.com/ufiles/imagecache/large-550px-centered/u1000009/quest-color-1.png) -图 3\. 玩家可以在游戏区域移动,比如围绕湖,通过山的通道到达未知的区域。 +*图 3. 玩家可以在游戏区域移动,比如围绕湖,通过山的通道到达未知的区域。* -通过颜色,你可以更清楚地展示信息。这个例子使用颜色指出可游戏的区域(绿色)相对着不可通过的区域(蓝色或者灰色)。我希望你可以使用这个示例游戏作为你自己的程序的一个起点或者参照。取决于你需要你的程序做什么,你可以通过curses做得更多。 +通过颜色,你可以更清楚地展示信息。这个例子使用颜色指出可游戏的区域(绿色)相对着不可通过的区域(蓝色或者灰色)。我希望你可以使用这个示例游戏作为你自己的程序的一个起点或者参照。这取决于你需要你的程序做什么,你可以通过 curses 做得更多。 -在下一篇文章,我计划展示ncurses库的其它特性,比如怎样创建窗口和边框。同时,如果你对于学习 curses 有兴趣,我建议你去读位于 [Linux 文档计划](http://www.tldp.org)的 Pradeep Padala 写的 [NCURSES Programming HOWTO](http://tldp.org/HOWTO/NCURSES-Programming-HOWTO)。 +在下一篇文章,我计划展示 ncurses 库的其它特性,比如怎样创建窗口和边框。同时,如果你对于学习 curses 有兴趣,我建议你去读位于 [Linux 文档计划](http://www.tldp.org) 的 Pradeep Padala 写的 [NCURSES Programming HOWTO](http://tldp.org/HOWTO/NCURSES-Programming-HOWTO)。 -------------------------------------------------------------------------------- @@ -379,11 +365,11 @@ via: http://www.linuxjournal.com/content/programming-color-ncurses 作者:[Jim Hall][a] 译者:[leemeans](https://github.com/leemeans) -校对:[校对者ID](https://github.com/校对者ID) +校对:[wxy](https://github.com/wxy) 本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 [a]:http://www.linuxjournal.com/users/jim-hall -[1]:http://www.linuxjournal.com/content/getting-started-ncurses -[2]:http://www.linuxjournal.com/content/creating-adventure-game-terminal-ncurses +[1]:https://linux.cn/article-9348-1.html +[2]:https://linux.cn/article-9383-1.html [3]:http://tldp.org/HOWTO/NCURSES-Programming-HOWTO \ No newline at end of file From c87ebabb53e615370708e513de9cb260b3bc18c6 Mon Sep 17 00:00:00 2001 From: wxy Date: Sun, 15 Apr 2018 09:33:02 +0800 Subject: [PATCH 10/15] PUB:20180206 Programming in Color with ncurses.md @leemeans https://linux.cn/article-9546-1.html --- .../20180206 Programming in Color with ncurses.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {translated/tech => published}/20180206 Programming in Color with ncurses.md (100%) diff --git a/translated/tech/20180206 Programming in Color with ncurses.md b/published/20180206 Programming in Color with ncurses.md similarity index 100% rename from translated/tech/20180206 Programming in Color with ncurses.md rename to published/20180206 Programming in Color with ncurses.md From 9aeb3bdd4d7b4de17edee04298ee9e7f03cc21ab Mon Sep 17 00:00:00 2001 From: MjSeven <33125422+MjSeven@users.noreply.github.com> Date: Sun, 15 Apr 2018 11:00:25 +0800 Subject: [PATCH 11/15] Update 20180412 3 password managers for the Linux command line.md --- .../20180412 3 password managers for the Linux command line.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sources/tech/20180412 3 password managers for the Linux command line.md b/sources/tech/20180412 3 password managers for the Linux command line.md index 578f4b386e..614528c131 100644 --- a/sources/tech/20180412 3 password managers for the Linux command line.md +++ b/sources/tech/20180412 3 password managers for the Linux command line.md @@ -1,3 +1,5 @@ +Translating by MjSeven + 3 password managers for the Linux command line ====== From 9f4e835019cfcaacd1138c085e4630f2c0010bca Mon Sep 17 00:00:00 2001 From: MZqk Date: Sun, 15 Apr 2018 12:03:16 +0800 Subject: [PATCH 12/15] translated by MZqk --- .../20180214 11 awesome vi tips and tricks.md | 100 ------------------ .../20180214 11 awesome vi tips and tricks.md | 95 +++++++++++++++++ 2 files changed, 95 insertions(+), 100 deletions(-) delete mode 100644 sources/talk/20180214 11 awesome vi tips and tricks.md create mode 100644 translated/talk/20180214 11 awesome vi tips and tricks.md diff --git a/sources/talk/20180214 11 awesome vi tips and tricks.md b/sources/talk/20180214 11 awesome vi tips and tricks.md deleted file mode 100644 index da31cc8318..0000000000 --- a/sources/talk/20180214 11 awesome vi tips and tricks.md +++ /dev/null @@ -1,100 +0,0 @@ -translating by MZqk - -11 awesome vi tips and tricks -====== - -![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/keyboaord_enter_writing_documentation.jpg?itok=kKrnXc5h) - -The [vi editor][1] is one of the most popular text editors on Unix and Unix-like systems, such as Linux. Whether you're new to vi or just looking for a refresher, these 11 tips will enhance how you use it. - -### Editing - -Editing a long script can be tedious, especially when you need to edit a line so far down that it would take hours to scroll to it. Here's a faster way. - - 1. The command `:set number` numbers each line down the left side. - -![](https://opensource.com/sites/default/files/styles/panopoly_image_original/public/u128651/setnum.png?itok=sFVA97mG) - -You can directly reach line number 26 by opening the file and entering this command on the CLI: `vi +26 sample.txt`. To edit line 26 (for example), the command `:26` will take you directly to it. - -![](https://opensource.com/sites/default/files/styles/panopoly_image_original/public/u128651/number.png?itok=d7FE0LL3) - -### Fast navigation - - 2. `i` changes your mode from "command" to "insert" and starts inserting text at the current cursor position. - 3. `a` does the same, except it starts just after the current cursor position. - 4. `o` starts the cursor position from the line below the current cursor position. - - - -### Delete - -If you notice an error or typo, being able to make a quick fix is important. Good thing vi has it all figured out. - -Understanding vi's delete function so you don't accidentally press a key and permanently remove a line, paragraph, or more, is critical. - - 5. `x` deletes the character under the cursor. - 6. `dd` deletes the current line. (Yes, the whole line!) - - - -Here's the scary part: `30dd` would delete 30 lines starting with the current line! Proceed with caution when using this command. - -### Search - -You can search for keywords from the "command" mode rather than manually navigating and looking for a specific word in a plethora of text. - - 7. `:/` searches for the word mentioned in the `< >` space and takes your cursor to the first match. - 8. To navigate to the next instance of that word, type `n`, and keep pressing it until you get to the match you're looking for. - - - -For example, in the image below I searched for `ssh`, and vi highlighted the beginning of the first result. - -![](https://opensource.com/sites/default/files/styles/panopoly_image_original/public/u128651/ssh-search.png?itok=tJ-7FujH) - -After I pressed `n`, vi highlighted the next instance. - -![](https://opensource.com/sites/default/files/styles/panopoly_image_original/public/u128651/n-search.png?itok=wU-u3LiI) - -### Save and exit - -Developers (and others) will probably find this next command useful. - - 9. `:x` saves your work and exits vi. - -![](https://opensource.com/sites/default/files/styles/panopoly_image_original/public/u128651/x.png?itok=kfoHx84m) - - 10. If you think every nanosecond is worth saving, here's a faster way to shift to terminal mode in vi. Instead of pressing `Shift+:` on the keyboard, you can press `Shift+q` (or Q, in caps) to access [Ex mode][2], but this doesn't really make any difference if you just want to save and quit by typing `x` (as shown above). - - - -### Substitution - -Here is a neat trick if you want to substitute every occurrence of one word with another. For example, if you want to substitute "desktop" with "laptop" in a large file, it would be monotonous and waste time to search for each occurrence of "desktop," delete it, and type "laptop." - - 11. The command `:%s/desktop/laptop/g` would replace each occurrence of "desktop" with "laptop" throughout the file; it works just like the Linux `sed` command. - - - -In this example, I replaced "root" with "user": - -![](https://opensource.com/sites/default/files/styles/panopoly_image_original/public/u128651/subs-command.png?itok=M8MN72sp) - -![](https://opensource.com/sites/default/files/styles/panopoly_image_original/public/u128651/subs-result.png?itok=34zzVdUt) - -These tricks should help anyone get started using vi. Are there other neat tips I missed? Share them in the comments. - --------------------------------------------------------------------------------- - -via: https://opensource.com/article/18/1/top-11-vi-tips-and-tricks - -作者:[Archit Modi][a] -译者:[译者ID](https://github.com/译者ID) -校对:[校对者ID](https://github.com/校对者ID) - -本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 - -[a]:https://opensource.com/users/architmodi -[1]:http://ex-vi.sourceforge.net/ -[2]:https://en.wikibooks.org/wiki/Learning_the_vi_Editor/Vim/Modes#Ex-mode diff --git a/translated/talk/20180214 11 awesome vi tips and tricks.md b/translated/talk/20180214 11 awesome vi tips and tricks.md new file mode 100644 index 0000000000..73606e9883 --- /dev/null +++ b/translated/talk/20180214 11 awesome vi tips and tricks.md @@ -0,0 +1,95 @@ +11 个超棒的 vi 技巧和窍门 +====== +![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/keyboaord_enter_writing_documentation.jpg?itok=kKrnXc5h) + +[vi][1] 编辑器是 Linux 最流行的编辑器之一,默认安装在 Unix 和类 Uinux 系统中。无论您是 vi 新手还是想进修,这些技巧都对您有用。 + +### 编辑 + +编辑长文本时可能很难受,特别是编辑其中某一行时,需要移动许久才能到这行。这有个很快的方法: + + 1. `:set number` 这个命令可是在编辑器左边显示行号。 + +![](https://opensource.com/sites/default/files/styles/panopoly_image_original/public/u128651/setnum.png?itok=sFVA97mG) + +您可以在命令行中输入 `vi +26 samp.txt` 命令直接打开文件到达 26 行,在 vi 编辑器中也可以输入 `:26` 跳转到 26 行。 + +![](https://opensource.com/sites/default/files/styles/panopoly_image_original/public/u128651/number.png?itok=d7FE0LL3) + +### 快速导航 + + 2. `i` 将工作方式从“命令模式”更改为“输入模式”,并在当前光标位置开始插入内容。 + 3. `a` 除了是光标之后开始插入年内容,与上面的效果是一样的。 + 4. `o` 在光标的下一行位置开始插入内容。 + + + +### 删除 + +如果您发现错误或错别字,能快速的修正是很重要的。好在 vi 都事先想好了。 + +了解 vi 的删除功能,保证你不会意外按下某个键并永久删除一行或多段内容,这点至关重要。 + + 5. `x` 删除当前光标的字符。 + 6. `dd` 删除当前行 (是的,整行内容!) + +下面看可怕的部分:`30dd` 从当前行开始删除以下 30 行!使用此命令请慎重。 + +### 搜索 + +您可以在“命令模式”搜索关键字,而不用在大量文本内容中手动导航查找特定的单词或内容。 + + 7. `:/` 搜索 `< >` 中的单词并将光标移动到第一个匹配项。 + 8. 导航到该单词的下一个匹配项,请输入 `n` 并继续按下, 直到找到您要找的内容。 + + + +例如,在这个图像中我要搜索包含 `ssh` 的内容, vi 光标就会突出第一个结果的开始位置。 + +![](https://opensource.com/sites/default/files/styles/panopoly_image_original/public/u128651/ssh-search.png?itok=tJ-7FujH) + +按下 n 之后, vi 光标就会突出下一个匹配项。 + +![](https://opensource.com/sites/default/files/styles/panopoly_image_original/public/u128651/n-search.png?itok=wU-u3LiI) + +### 保存并退出 + +开发人员 (或其他人) 可能会发现这个命令很有用。 + + 9. `:x` 保存您的工作空间并退出 vi 。 + +![](https://opensource.com/sites/default/files/styles/panopoly_image_original/public/u128651/x.png?itok=kfoHx84m) + + 10. 如果你想实时保存,那么这有个更快的方法。你可以按下 `Shift+q` (或者大写字母 Q ) 来进入 [Ex 模式][2] , 而不是在键盘上按 `Shift+:` 。但是如果你只是想按下 `x` 来保存退出,那就没有什么区别(如上所示)。 + + + +### 替换 + +如果您想将文中的某个单词全部替换为一个单词,这有个很巧妙的招式。例如,如果您想在一个大文件中将 "desktop" 替换为 "laptop" ,那么单调的搜索每个出现的 "desktop" 将其删掉,然后再输入 "laotop" ,是很浪费时间的。 + + 11. `:%s/desktop/laptop/g` 这个命令将在整个文件中的 "desktop" 用 "laptop" 替换,他就像 Linux 的 `sed` 命令一样。 + + + +这个例子中我用 "user" 替换了 "root" : + +![](https://opensource.com/sites/default/files/styles/panopoly_image_original/public/u128651/subs-command.png?itok=M8MN72sp) + +![](https://opensource.com/sites/default/files/styles/panopoly_image_original/public/u128651/subs-result.png?itok=34zzVdUt) + +这些技巧应该能帮组任何想开始学 vi 的人。我有遗漏其他巧妙的提示吗?请在评论中分享他们。 + +-------------------------------------------------------------------------------- + +via: https://opensource.com/article/18/1/top-11-vi-tips-and-tricks + +作者:[Archit Modi][a] +译者:[MZqk](https://github.com/MZqk) +校对:[校对者 ID](https://github.com/ 校对者 ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux 中国](https://linux.cn/) 荣誉推出 + +[a]:https://opensource.com/users/architmodi +[1]:http://ex-vi.sourceforge.net/ +[2]:https://en.wikibooks.org/wiki/Learning_the_vi_Editor/Vim/Modes#Ex-mode From 1b9ee26cbd0f5b54ac25ba0b38d68b8f431a96e2 Mon Sep 17 00:00:00 2001 From: zyk Date: Sun, 15 Apr 2018 13:02:16 +0800 Subject: [PATCH 13/15] Translating by zyk2290 --- .../20180115 2 scientific calculators for the Linux desktop.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sources/tech/20180115 2 scientific calculators for the Linux desktop.md b/sources/tech/20180115 2 scientific calculators for the Linux desktop.md index f91450b383..8b993a1aae 100644 --- a/sources/tech/20180115 2 scientific calculators for the Linux desktop.md +++ b/sources/tech/20180115 2 scientific calculators for the Linux desktop.md @@ -1,6 +1,8 @@ 2 scientific calculators for the Linux desktop ====== +Translating by zyk2290 + ![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/osdc_OpenData_CityNumbers.png?itok=lC03ce76) Image by : opensource.com From efb875418925d064586e04e227b2cc19df665dec Mon Sep 17 00:00:00 2001 From: hopefully2333 <787016457@qq.com> Date: Sun, 15 Apr 2018 13:36:06 +0800 Subject: [PATCH 14/15] translated by hopefully2333 --- .../tech/20180411 Awesome GNOME extensions for developers.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sources/tech/20180411 Awesome GNOME extensions for developers.md b/sources/tech/20180411 Awesome GNOME extensions for developers.md index d98b749af9..3367dcf216 100644 --- a/sources/tech/20180411 Awesome GNOME extensions for developers.md +++ b/sources/tech/20180411 Awesome GNOME extensions for developers.md @@ -1,3 +1,5 @@ +translated by hopefully2333 + Awesome GNOME extensions for developers ====== From 3b46fb1644799cee69e8bd4e8d020253ede33c87 Mon Sep 17 00:00:00 2001 From: cmn <2545489745@qq.com> Date: Sun, 15 Apr 2018 15:02:30 +0800 Subject: [PATCH 15/15] translated --- ...nuous deployment with Github and Python.md | 186 ------------------ ...nuous deployment with Github and Python.md | 185 +++++++++++++++++ 2 files changed, 185 insertions(+), 186 deletions(-) delete mode 100644 sources/tech/20180328 Do continuous deployment with Github and Python.md create mode 100644 translated/tech/20180328 Do continuous deployment with Github and Python.md diff --git a/sources/tech/20180328 Do continuous deployment with Github and Python.md b/sources/tech/20180328 Do continuous deployment with Github and Python.md deleted file mode 100644 index 0452f8c669..0000000000 --- a/sources/tech/20180328 Do continuous deployment with Github and Python.md +++ /dev/null @@ -1,186 +0,0 @@ -translating by kimii -Do continuous deployment with Github and Python -====== - -![](https://fedoramagazine.org/wp-content/uploads/2018/03/cd-github-python-945x400.jpg) -Developers can create many useful services using Github’s webhooks. From triggering a CI job on a Jenkins instance to provisioning machines in the cloud, the possibilities are almost limitless. This tutorial shows how to use Python and the Flask framework to build a simple continuous deployment service. - -The continuous deployment service in this example is a simple Flask application with a REST endpoint that will receive Github’s webhook requests. After validating each request to check that it comes from the correct Github repository, the service pulls changes to the local copy of the repository. That way every time a new commit is pushed to the remote Github repository, the local repository is automatically updated. - -### Flask web service - -It is easy to build a small web service with Flask. Here’s a look at the project structure. -``` -├── app -│   ├── __init__.py -│   └── webhooks.py -├── requirements.txt -└── wsgi.py - -``` - -First, create the application. The application code goes under the app directory. - -Two files ( __init__.py and webhooks.py) compose the Flask application. The former has the code needed to create the Flask application and add configuration to it. The latter has the endpoints logic. This is where the app receives the data from the Github request. - -Here is the app/__init__.py content: -``` -import os -from flask import Flask - -from .webhooks import webhook - -def create_app(): - """ Create, configure and return the Flask application """ - - app = Flask(__name__) - app.config['GITHUB_SECRET'] = os.environ.get('GITHUB_SECRET') - app.config['REPO_PATH'] = os.environ.get('REPO_PATH') - app.register_blueprint(webhook) - - return(app) - -``` - -The function creates two configuration variables: - - * **GITHUB_SECRET** holds a secret passphrase, used to authenticate the Github requests. - * **REPO_PATH** holds the path of the repository to automatically update. - - - -This code uses [Flask Blueprints][1] to organize the application endpoints. Using blueprints allows logical grouping of APIs, making applications easier to maintain. It is generally considered a good practice. - -Here is the content of app/webhooks.py: -``` -import hmac -from flask import request, Blueprint, jsonify, current_app -from git import Repo - -webhook = Blueprint('webhook', __name__, url_prefix='') - -@webhook.route('/github', methods=['POST']) -def handle_github_hook(): - """ Entry point for github webhook """ - - signature = request.headers.get('X-Hub-Signature') - sha, signature = signature.split('=') - - secret = str.encode(current_app.config.get('GITHUB_SECRET')) - - hashhex = hmac.new(secret, request.data, digestmod='sha1').hexdigest() - if hmac.compare_digest(hashhex, signature): - repo = Repo(current_app.config.get('REPO_PATH')) - origin = repo.remotes.origin - origin.pull('--rebase') - - commit = request.json['after'][0:6] - print('Repository updated with commit {}'.format(commit)) - return jsonify({}), 200 - -``` - -First the code creates a new Blueprint webhook. Then it adds a new endpoint to the Blueprint using a Flask route. This route will be called by any POST request on the /github URL endpoint. - -#### Verifying the request - -When the service receives a request on this endpoint, it must first verify that the request comes from Github and from the correct repository. Github gives a signature in the request header X-Hub-Signature. This signature is generated using a secret (GITHUB_SECRET), the [HMAC][2] hex digest of the request body, and then hashed using the sha1 hash function. - -To verify the request the service needs to calculate locally the signature and compare it to the signature received in the request header. This is done by the hmac.compare_digest function. - -#### Custom hook logic - -After validating the request, it can now be processd. This tutorial uses the [GitPython][3] module to interface with a git repository. From the GitPython module the Repo object is used to access the remote repository called origin. The service pulls the latest changes locally from the origin repository, also using the –rebase option to avoid issues with merges. - -A debug print statement displays the short commit hash received from the request body. This example shows how to use the request body. For more details about the data available in the body, check [github’s documentation][4]. - -Finally the service returns a empty JSON string and a 200 status code. This tells Github’s webhook server the request was received. - -### Deploying the service - -To run the service, this example uses the [gunicorn][5] web server. First install the service dependencies. On a supported Fedora server, use this command with [sudo][6]: -``` -sudo dnf install python3-gunicorn python3-flask python3-GitPython - -``` - -Now edit the wsgi.py file used by gunicorn to run the service: -``` -from app import create_app - -application = create_app() - -``` - -To deploy this service, clone this git [repository][7] or use your own git repository with this command: -``` -git clone https://github.com/cverna/github_hook_deployment.git /opt/ - -``` - -The next step is to configure the environment variables needed by the service. Run these commands: -``` -export GITHUB_SECRET=asecretpassphraseusebygithubwebhook -export REPO_PATH=/opt/github_hook_deployment/ - -``` - -This tutorial uses the webhook service Github repository, but you could use a different repository if you wish. Finally, start the webserver with these commands: -``` -cd /opt/github_hook_deployment/ -gunicorn --bind 0.0.0.0 wsgi:application --reload - -``` - -These options bind the web server to the 0.0.0.0 ip address, meaning it will accept requests coming from any host. The –reload option ensures the web server restarts when the code changes. This is where the continuous deployment magic happens. Every Github request received pulls the latest change in the repository, and gunicorn detects these changes and automatically restarts the application. - -**Note: **In order to receive the requests from github, the web service must be deployed on a server with a public IP address. An easy way to do this is to use your favorite cloud provider such as DigitalOcean, AWS, Linode, etc. - -### Configure Github - -The last part of this tutorial configures Github to send the webhook request to the web service. This is key to continuous deployment. - -From your Github repository settings, select the Webhook menu and click on Add Webhook. Enter the following information: - - * **Payload URL:** The URL of the service, for example - * **Content type:** Select application/json - * **Secret:** The **GITHUB_SECRET** environment variable defined earlier - - - -Then click the Add Webhook button. - -![][8] - -Github will now send a request to the service every time a push event happens on this repository. - -### Conclusion - -This tutorial has shown you how to write a Flask based web service that receives requests from a Github webhook and does continuous deployment. You should now be able to build your own useful services using this tutorial as a starting base. - -#### Like this: - -Like - -Loading... - --------------------------------------------------------------------------------- - -via: https://fedoramagazine.org/continuous-deployment-github-python/ - -作者:[Author Archive;Author Website;Clément Verna][a] -译者:[译者ID](https://github.com/译者ID) -校对:[校对者ID](https://github.com/校对者ID) -选题:[lujun9972](https://github.com/lujun9972) - -本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 - -[a]:https://fedoramagazine.org -[1]:http://flask.pocoo.org/docs/0.12/blueprints/ -[2]:https://en.wikipedia.org/wiki/HMAC -[3]:https://gitpython.readthedocs.io/en/stable/index.html -[4]:https://developer.github.com/v3/activity/events/types/#webhook-payload-example-26 -[5]:http://gunicorn.org/ -[6]:https://fedoramagazine.org/howto-use-sudo/ -[7]:https://github.com/cverna/github_hook_deployment.git -[8]:https://fedoramagazine.org/wp-content/uploads/2018/03/Screenshot-2018-3-26-cverna-github_hook_deployment1.png diff --git a/translated/tech/20180328 Do continuous deployment with Github and Python.md b/translated/tech/20180328 Do continuous deployment with Github and Python.md new file mode 100644 index 0000000000..a027282e72 --- /dev/null +++ b/translated/tech/20180328 Do continuous deployment with Github and Python.md @@ -0,0 +1,185 @@ +使用 Github 和 Python 实现持续部署 +====== + +![](https://fedoramagazine.org/wp-content/uploads/2018/03/cd-github-python-945x400.jpg) +借助 Github 的 webhook(网络钩子)开发者可以创建很多有用的服务。从触发一个 Jenkins 实例上的 CI(持续集成) 任务到配置云中的机器,几乎有着无限的可能性。这篇教程将展示如何使用 Python 和 Flask 框架来搭建一个简单的持续部署服务。 + +在这个例子中的持续部署服务是一个简单的 Flask 应用,其带有接受 Github 的 webhook 请求的 REST 端点(endpoint)。在验证每个请求都来自正确的 Github 仓库后,服务器将拉取(pull)更改到仓库的本地副本。这样每次一个新的提交(commit)推送到远程 Github 仓库,本地仓库就会自动更新。 + +### Flask web 服务 + +用 Flask 搭建一个小的 web 服务非常简单。这里可以先看看项目的结构。 +``` +├── app +│ ├── __init__.py +│ └── webhooks.py +├── requirements.txt +└── wsgi.py + +``` + +首先,创建应用。应用代码在 app 目录下。 + +两个文件(__init__.py 和 webhooks.py)构成了 Flask 应用。前者有创建 Flask 应用并为其添加配置的代码。后者有端点(endpoint)逻辑。这是该应用接收 Github 请求的地方。 + +这里是 app/__init__.py 的内容: +``` +import os +from flask import Flask + +from .webhooks import webhook + +def create_app(): + """ Create, configure and return the Flask application """ + + app = Flask(__name__) + app.config['GITHUB_SECRET'] = os.environ.get('GITHUB_SECRET') + app.config['REPO_PATH'] = os.environ.get('REPO_PATH') + app.register_blueprint(webhook) + + return(app) + +``` + +该函数创建了两个配置变量: + + * **GITHUB_SECRET** 保存一个密码,用来认证 Github 请求。 + * **REPO_PATH** 保存了自动更新的仓库路径。 + + + +这份代码使用[Flask 蓝图(Blueprints)][1]来组织应用的端点(endpoint)。使用蓝图可以对 API 进行逻辑分组,使应用程序更易于维护。通常认为这是一种好的做法。 + +这里是 app/webhooks.py 的内容: +``` +import hmac +from flask import request, Blueprint, jsonify, current_app +from git import Repo + +webhook = Blueprint('webhook', __name__, url_prefix='') + +@webhook.route('/github', methods=['POST']) +def handle_github_hook(): + """ Entry point for github webhook """ + + signature = request.headers.get('X-Hub-Signature') + sha, signature = signature.split('=') + + secret = str.encode(current_app.config.get('GITHUB_SECRET')) + + hashhex = hmac.new(secret, request.data, digestmod='sha1').hexdigest() + if hmac.compare_digest(hashhex, signature): + repo = Repo(current_app.config.get('REPO_PATH')) + origin = repo.remotes.origin + origin.pull('--rebase') + + commit = request.json['after'][0:6] + print('Repository updated with commit {}'.format(commit)) + return jsonify({}), 200 + +``` + +首先代码创建了一个新的蓝图 webhook。然后它使用 Flask 路由(route)为蓝图添加了一个端点。任何 URL 为 /Github 的端点的 POST 请求都将调用这个路由。 + +#### 验证请求 + +当服务收到该端点的请求时,首先它必须验证该请求是否来自 GitHub 同时来自正确的仓库。Github 在请求头的 X-Hub-Signature 中提供了一个签名。该签名由一个密码(GITHUB_SECRET),请求体的 HMAC 十六进制摘要和使用 sha1 的哈希生成。 + +为了验证请求,服务需要在本地计算签名并与请求头中收到的签名做比较。这可以由 hmac.compare_digest 函数完成。 + +#### 自定义钩子逻辑 + +在验证请求后,现在可以处理了。这篇教程使用[GitPython][3]模块来与 git 仓库进行交互。GitPython 模块中的 Repo 对象用于访问远程仓库 origin。服务在本地拉取远程仓库的最新更改,还用 -rebase 选项来避免合并的问题。 + +调试打印语句显示了从请求体收到的短提交哈希。这个例子展示了如何使用请求体。更多关于请求体的可用数据的信息,请查询[github 文档][4]。 + +最后服务返回了一个空的 JSON 字符串和 200 的状态码。这用于告诉 Github 的 webhook 服务已经收到了请求。 + +### 部署服务 + +为了运行该服务,这个例子使用 [gunicorn][5] web 服务器。首先安装服务依赖。在支持的 Fedora 服务器上,以[sudo][6]运行这条命令: +``` +sudo dnf install python3-gunicorn python3-flask python3-GitPython + +``` + +现在编辑 gunicorn 使用的 wsgi.py 文件来运行服务: +``` +from app import create_app + +application = create_app() + +``` + +为了部署服务,使用以下命令克隆这个 git [仓库][7]或者使用你自己的 git 仓库: +``` +git clone https://github.com/cverna/github_hook_deployment.git /opt/ + +``` + +下一步是配置服务所需的环境变量。运行这些命令: +``` +export GITHUB_SECRET=asecretpassphraseusebygithubwebhook +export REPO_PATH=/opt/github_hook_deployment/ + +``` + +这篇教程使用 webhook 服务的 Github 仓库,但你可以使用你想要的不同仓库。最后,使用这些命令开启 该 web 服务: +``` +cd /opt/github_hook_deployment/ +gunicorn --bind 0.0.0.0 wsgi:application --reload + +``` + +这些选项中绑定了 web 服务的 ip 地址为 0.0.0.0,意味着它将接收来自任何的主机的请求。选项 -reload 确保了当代码更改时重启 web 服务。这就是持续部署的魔力所在。每次接收到 Github 请求时将拉取仓库的最近更新,同时 gunicore 检测这些更改并且自动重启服务。 + +**注意: **为了能接收到 Github 请求,web 服务必须部署到具有公有 IP 地址的服务器上。做到这点的简单方法就是使用你最喜欢的云提供商比如 DigitalOcean,AWS,Linode等。 + +### 配置 Github + +这篇教程的最后一部分是配置 Github 来发送 webhook 请求到 web 服务上。这是持续部署的关键。 + +从你的 Github 仓库的设置中,选择 Webhook 菜单,并且点击添加 Webhook。输入以下信息: + + * **Payload URL:** 服务的 URL,比如 + * **Content type:** 选择 application/json + * **Secret:** 前面定义的 **GITHUB_SECRET** 环境变量 + + + +然后点击添加 Webhook 按钮。 + +![][8] + +现在每当该仓库发生 push(推送)事件时,Github 将向服务发送请求。 + +### 总结 + +这篇教程向你展示了如何写一个基于 Flask 的用于接收 Github 的 webhook 请求和实现持续集成的 web 服务。现在你应该能以本教程作为起点来搭建对自己有用的服务。 + +#### 像这样: + +如 + +加载中... + +-------------------------------------------------------------------------------- + +via: https://fedoramagazine.org/continuous-deployment-github-python/ + +作者:[Author Archive;Author Website;Clément Verna][a] +译者:[kimii](https://github.com/kimii) +校对:[校对者ID](https://github.com/校对者ID) +选题:[lujun9972](https://github.com/lujun9972) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]:https://fedoramagazine.org +[1]:http://flask.pocoo.org/docs/0.12/blueprints/ +[2]:https://en.wikipedia.org/wiki/HMAC +[3]:https://gitpython.readthedocs.io/en/stable/index.html +[4]:https://developer.github.com/v3/activity/events/types/#webhook-payload-example-26 +[5]:http://gunicorn.org/ +[6]:https://fedoramagazine.org/howto-use-sudo/ +[7]:https://github.com/cverna/github_hook_deployment.git +[8]:https://fedoramagazine.org/wp-content/uploads/2018/03/Screenshot-2018-3-26-cverna-github_hook_deployment1.png